HDU 1198 Farm Irrigation (并查集)
2014-08-13 10:57
204 查看
Farm Irrigation
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 5809 Accepted Submission(s): 2516
[align=left]Problem Description[/align]
Benny has a spacious farm land to irrigate. The farm land is a rectangle, and is divided into a lot of samll squares. Water pipes are placed in these squares. Different square has a different type of pipe. There are 11 types of pipes, which is marked from A to K, as Figure 1 shows.
#include<cstdio> #include<cstring> #include<stdlib.h> #include<algorithm> using namespace std; const int MAXN=55; char ch; int a[MAXN][MAXN],p[MAXN*MAXN]; int dir[4][2]={{-1,0},{0,1},{1,0},{0,-1}};//寻找方向:上右下左 int map[11][4]={{1,0,0,1},//从上右下左四个方向依次列出每一个方块的连通情况 {1,1,0,0}, {0,0,1,1}, {0,1,1,0}, {1,0,1,0}, {0,1,0,1}, {1,1,0,1}, {1,0,1,1}, {0,1,1,1}, {1,1,1,0}, {1,1,1,1} }; int Find(int x) { return p[x]==x?x:p[x]=Find(p[x]); } void Union(int a,int b) { int x=Find(a); int y=Find(b); if(x!=y) p[x]=y; } int main() { //freopen("in.txt","r",stdin); int n,m; while(scanf("%d %d%*c",&m,&n)&&m>0) { for(int i=0;i<m;i++) { for(int j=0;j<n;j++) { scanf("%c",&ch); a[i][j]=ch-'A'; } getchar(); } for(int i=0;i<n*m;i++) p[i]=i; for(int i=0;i<m;i++) { for(int j=0;j<n;j++) { for(int k=0;k<4;k++) { int next_x,next_y; next_x=i+dir[k][0]; next_y=j+dir[k][1]; if(0<=next_x&&next_x<m&&0<=next_y&&next_y<n) { if(k==0) { if(map[a[next_x][next_y]][2]&&map[a[i][j]][0]) Union(next_x*n+next_y,i*n+j); } if(k==1) { if(map[a[next_x][next_y]][3]&&map[a[i][j]][1]) Union(next_x*n+next_y,i*n+j); } if(k==2) { if(map[a[next_x][next_y]][0]&&map[a[i][j]][2]) Union(next_x*n+next_y,i*n+j); } if(k==3) { if(map[a[next_x][next_y]][1]&&map[a[i][j]][3]) Union(next_x*n+next_y,i*n+j); } } } } } // for(int i=0;i<n*m;i++) // printf("%d ",p[i]); // printf("\n"); int cnt=0; for(int i=0;i<n*m;i++) if(p[i]==i) cnt++; printf("%d\n",cnt); } }
View Code
相关文章推荐
- HDU 1198 Farm Irrigation(并查集)
- Farm Irrigation HDU - 1198 (并查集)
- HDU 1198 Farm Irrigation(并查集)
- HDU 1198 Farm Irrigation (并查集)
- hdu 1198 Farm Irrigation ( 打表+并查集)
- hdu 1198 Farm Irrigation(并查集)
- HDU 1198-Farm Irrigation(并查集)
- HDU 1198 Farm Irrigation(并查集)
- hdu 1198 Farm Irrigation(并查集)
- hdu 1198 Farm Irrigation(并查集)
- hdu 1198 Farm Irrigation(并查集)
- hdu 1198 Farm Irrigation(并查集)
- HDU 1198 Farm Irrigation(并查集+位运算)
- HDU 1198 Farm Irrigation(并查集)
- hdu-1198-Farm Irrigation(并查集)
- hdu 1198 Farm Irrigation 搜索/并查集
- hdu 1198 Farm Irrigation(并查集)
- hdu 1198 Farm Irrigation( 并查集)
- HDU_1198 Farm Irrigation(并查集)
- hdu 1198 Farm Irrigation(深搜dfs || 并查集)