hdu Farm Irrigation
这题第一感觉是用搜索做,暴力就可以解决,这里将水管转换成一个个3*3的矩阵,然后搜素就可以了。写完之后确实一遍过掉了,31ms。附上代码:
#include"iostream"
#include"stdio.h"
#include"algorithm"
#include"cmath"
#include"queue"
#include"stack"
#include"vector"
#include"string"
#include"string.h"
using namespace std;
const int mx=;
int A[][]={{,,},{,,},{,,}};
int B[][]={{,,},{,,},{,,}};
int C[][]={{,,},{,,},{,,}};
int D[][]={{,,},{,,},{,,}};
int E[][]={{,,},{,,},{,,}};
int F[][]={{,,},{,,},{,,}};
int G[][]={{,,},{,,},{,,}};
int H[][]={{,,},{,,},{,,}};
int I[][]={{,,},{,,},{,,}};
int J[][]={{,,},{,,},{,,}};
int K[][]={{,,},{,,},{,,}};
int dir[][]={{,},{,},{,-},{-,}};
int maze[mx][mx];
bool visited[mx][mx];
int m,n,wellspring;
bool judge(int x,int y)
{
if(x>=&&x<*m&&y>=&&y<*n&&maze[x][y]==&&!visited[x][y])
return true;
return false;
}
void dfs(int x,int y)
{
int dx,dy;
for(int i=;i<;i++)
{
dx=x+dir[i][];
dy=y+dir[i][];
if(judge(dx,dy))
{
visited[dx][dy]=true;
dfs(dx,dy);
}
}
}
void creat_maze(int a[][],int i,int j)
{
int p,q;
for(p=i*;p<i*+;p++)
{
for(q=j*;q<j*+;q++)
{
maze[p][q]=a[p-i*][q-j*];
}
}
}
void wellspring_count()
{
int i,j;
for(i=;i<*m;i++)
{
for(j=;j<*n;j++)
{
if(maze[i][j]==&&!visited[i][j])
{
wellspring++;
visited[i][j]=true;
dfs(i,j);
}
}
}
printf("%d\n",wellspring);
}
void input()
{
int i,j;
while(scanf("%d%d",&m,&n),m!=-||n!=-)
{
memset(visited,false,sizeof(visited));
wellspring=;
char pipe;
for(i=;i<m;i++)
{
getchar();
for(j=;j<n;j++)
{
scanf("%c",&pipe);
switch(pipe)
{
case 'A': creat_maze(A,i,j);break;
case 'B': creat_maze(B,i,j);break;
case 'C': creat_maze(C,i,j);break;
case 'D': creat_maze(D,i,j);break;
case 'E': creat_maze(E,i,j);break;
case 'F': creat_maze(F,i,j);break;
case 'G': creat_maze(G,i,j);break;
case 'H': creat_maze(H,i,j);break;
case 'I': creat_maze(I,i,j);break;
case 'J': creat_maze(J,i,j);break;
case 'K': creat_maze(K,i,j);break;
}
}
}
wellspring_count();
}
} int main()
{
// freopen("E:\\in.txt","r",stdin);
input();
return ;
}
但是这是一个并查集专题里的题,所以应该还会有更高效的方法,毕竟我的代码里没有用到并查集。
所以又想了一下,瞬变看了一下别人的并查集思路,又写了一个代码。然而,这次用并查集却死活过不了。。。找bug找了接近两个小时了,自己找的案例都过掉了。。。
先附上wa掉的代码:
#include"iostream"
#include"stdio.h"
#include"string"
#include"string.h"
#include"cmath"
#include"algorithm"
#include"ctype.h"
#include"queue"
#include"stack"
#include"vector"
using namespace std;
const int mx=;
int maze[mx][mx];
int fa[mx*mx];
int m,n,wellspring;
int dir[][]={{,-},{-,},{,},{,}};//左上右下
//左上右下有水管接口的用1表示
int farm[][]=
{
{,,,},
{,,,},
{,,,},
{,,,},
{,,,},
{,,,},
{,,,},
{,,,},
{,,,},
{,,,},
{,,,}
};
void Set()
{
int i;
for(i=;i<m*n;i++)
fa[i]=i;
} int Find(int x)
{
int t1,t2=x;
while(t2!=fa[t2]) t2=fa[t2];
return t2;
} void Union(int x,int y)
{
int fx=Find(x);
int fy=Find(y);
if(fx!=fy)
{
fa[fx]=fy;
wellspring--;
}
} bool judge(int k,int x,int y)
{
switch(k)
{
case :
if(farm[x][]&&farm[y][])
return true;
break;
case :
if(farm[x][]&&farm[y][])
return true;
break;
case :
if(farm[x][]&&farm[y][])
return true;
break;
case :
if(farm[x][]&&farm[y][])
return true;
break;
}
return false;
} void Count()
{
int i,j,k,dx,dy;
for(i=;i<m;i++)
{
for(j=;j<n;j++)
{
for(k=;k<;k++)
{
dx=i+dir[k][];
dy=j+dir[k][];
if(dx>=&&dx<m&&dy>=&&dy<n)//判定越界条件
if(judge(k,maze[i][j],maze[dx][dy]))
Union(i*m+j,dx*m+dy);
}
}
}
} void Input()
{
int i,j;
while(scanf("%d%d",&m,&n),m>=&&n>=)
{
Set();
wellspring=m*n;
char pipe;
for(i=;i<m;i++)
{
getchar();
for(j=;j<n;j++)
{
scanf("%c",&pipe);
maze[i][j]=pipe-'A';
}
}
Count();
printf("%d\n",wellspring);
}
} int main()
{
// freopen("E:\\in.txt","r",stdin);
Input();
return ;
}
hdu Farm Irrigation的更多相关文章
- HDU 1198 Farm Irrigation(状态压缩+DFS)
题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=1198 题目: Farm Irrigation Time Limit: 2000/1000 MS (Ja ...
- hdu.1198.Farm Irrigation(dfs +放大建图)
Farm Irrigation Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- HDU 1198 Farm Irrigation (并检查集合 和 dfs两种实现)
Farm Irrigation Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- HDU 1198 Farm Irrigation(并查集,自己构造连通条件或者dfs)
Farm Irrigation Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- HDU 1198 Farm Irrigation(并查集+位运算)
Farm Irrigation Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Tot ...
- ZOJ 2412 Farm Irrigation
Farm Irrigation Time Limit: 2 Seconds Memory Limit: 65536 KB Benny has a spacious farm land to ...
- HDU1198水管并查集Farm Irrigation
Benny has a spacious farm land to irrigate. The farm land is a rectangle, and is divided into a lot ...
- ZOJ 2412 Farm Irrigation(DFS 条件通讯块)
意甲冠军 两个农田管内可直接连接到壳体 他们将能够共享一个水源 有11种农田 管道的位置高于一定 一个农田矩阵 问至少须要多少水源 DFS的连通块问题 两个相邻农田的管道能够直接连接的 ...
- 【简单并查集】Farm Irrigation
Farm Irrigation Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Tot ...
随机推荐
- Oracle资源
ORACLE 10g下载地址 oracle 下载还需要用户名我自己注册了个方便大家使用下载 user:1603869780@qq.compass:qwe123QWE现在直接点击不能下载了 要经过ora ...
- 序列化 Serializable
1.序列化是干什么的? 简单说就是为了保存在内存中的各种对象的状态(也就是实例变量,不是方法),并且可以把保存的对象状态再读出来.虽然你可以用你自己的各种各样的方法来保存object states,但 ...
- html css js 一些记录.
webstorm 的基本使用 webstorm 格式化 html 代码: Ctrl+Alt+L js html css 基本使用 注意 dom 的 innerHTML会刷新dom,所以里面包含的事件绑 ...
- 【MyEcplise SVN】myEcplise上安装SVN的多种方式
第一种:SVN的在线安装 1.打开MyEclipse,找到顶部菜单栏 Help(帮助)-Install from Site-(从网站安装),如下图 2. 然后: 点击Install from Site ...
- Oracle PL/SQL设置快捷键的方法
pl sql默认设置不是很方便,最近搜罗了一下网上关于PLSQL的一些常用快捷键配置,主要是方便以后自个使用 1.登录后默认自动选中My Objects 默认情况下,PLSQL Developer ...
- unity 常用函数
GameObject.FindGameObjectByTag(); anim.SetFloat("speed",Mathf.Abs(h)); Physics2D.lineCast2 ...
- BZOJ3564 : [SHOI2014]信号增幅仪
先把所有点绕原点逆时针旋转(360-a)度,再把所有点横坐标除以放大倍数p,最后用随机增量法求最小圆覆盖即可. 时间复杂度期望$O(n)$ #include<cstdio> #includ ...
- js定时相关函数:
定时相关函数: mytime= setTimeout(vCode, iMilliSeconds [, sLanguage]) -- 单次定时执行指定函数 clearTimeout(iTimeoutID ...
- (转)MySQL提示“too many connections”的解决办法
link:http://www.cfp8.com/mysql-prompt-too-many-connections-solution.html 今天生产服务器上的MySQL出现了一个不算太陌生的错误 ...
- <fieldset>
legend{text-align:center;} <fieldset> <legend>爱好<legend>(为fieldset定义标题) <input ...