HDU1213——Farm Irrigation(并查集)
2015-09-17 20:45
323 查看
Problem Description
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.
Figure 1
Benny has a map of his farm, which is an array of marks denoting the distribution of water pipes over the whole farm. For example, if he has a map
ADC
FJK
IHE
then the water pipes are distributed like
Figure 2
Several wellsprings are found in the center of some squares, so water can flow along the pipes from one square to another. If water flow crosses one square, the whole farm land in this square is irrigated and will have a good harvest in autumn.
Now Benny wants to know at least how many wellsprings should be found to have the whole farm land irrigated. Can you help him?
Note: In the above example, at least 3 wellsprings are needed, as those red points in Figure 2 show.
Input
There are several test cases! In each test case, the first line contains 2 integers M and N, then M lines follow. In each of these lines, there are N characters, in the range of 'A' to 'K', denoting the type of water pipe over the corresponding square. A negative
M or N denotes the end of input, else you can assume 1 <= M, N <= 50.
Output
For each test case, output in one line the least number of wellsprings needed.
Sample Input
Sample Output
对每张图片,按照顺序,排在当前图片的右边或下边可以联通的,记为1,不能联通记为0
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.
Figure 1
Benny has a map of his farm, which is an array of marks denoting the distribution of water pipes over the whole farm. For example, if he has a map
ADC
FJK
IHE
then the water pipes are distributed like
Figure 2
Several wellsprings are found in the center of some squares, so water can flow along the pipes from one square to another. If water flow crosses one square, the whole farm land in this square is irrigated and will have a good harvest in autumn.
Now Benny wants to know at least how many wellsprings should be found to have the whole farm land irrigated. Can you help him?
Note: In the above example, at least 3 wellsprings are needed, as those red points in Figure 2 show.
Input
There are several test cases! In each test case, the first line contains 2 integers M and N, then M lines follow. In each of these lines, there are N characters, in the range of 'A' to 'K', denoting the type of water pipe over the corresponding square. A negative
M or N denotes the end of input, else you can assume 1 <= M, N <= 50.
Output
For each test case, output in one line the least number of wellsprings needed.
Sample Input
2 2 DK HF 3 3 ADC FJK IHE -1 -1
Sample Output
2 3
对每张图片,按照顺序,排在当前图片的右边或下边可以联通的,记为1,不能联通记为0
#include<iostream> #include<cstring> #include<cstdio> using namespace std; int Right[11][11]= {{0,0,0,0,0,0,0,0,0,0,0}, {1,0,1,0,0,1,1,1,1,0,1},{0,0,0,0,0,0,0,0,0,0,0}, {1,0,1,0,0,1,1,1,1,0,1},{0,0,0,0,0,0,0,0,0,0,0}, {1,0,1,0,0,1,1,1,1,0,1},{1,0,1,0,0,1,1,1,1,0,1}, {0,0,0,0,0,0,0,0,0,0,0},{1,0,1,0,0,1,1,1,1,0,1}, {1,0,1,0,0,1,1,1,1,0,1},{1,0,1,0,0,1,1,1,1,0,1} }; int Down[11][11]= {{0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0},{1,1,0,0,1,0,1,1,0,1,1}, {1,1,0,0,1,0,1,1,0,1,1},{1,1,0,0,1,0,1,1,0,1,1}, {0,0,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,0,0}, {1,1,0,0,1,0,1,1,0,1,1},{1,1,0,0,1,0,1,1,0,1,1}, {1,1,0,0,1,0,1,1,0,1,1},{1,1,0,0,1,0,1,1,0,1,1} }; int p[3000]; char map[55][55]; int find(int i) { int j=i; while(j!=p[j]) { j=p[j]; } return p[j]=j; } void set(int x,int y) { x=find(x); y=find(y); if(x!=y) { p[x]=y; } } int main() { int i,j,m,n,cnt; while(scanf("%d%d",&m,&n)) { if(m==-1&&n==-1) break; for(i=0; i<m*n; ++i) p[i]=i; memset(map,0,sizeof(map)); for(i=0; i<m; ++i) scanf("%s",&map[i]); for(i=0; i<m; ++i) for(j=1; j<n; ++j) if(Right[map[i][j-1]-'A'][map[i][j]-'A']) set(i*n+j-1,i*n+j); for(i=0; i<n; ++i) for(j=1; j<m; ++j) if(Down[map[j-1][i]-'A'][map[j][i]-'A']) set((j-1)*n+i,j*n+i); cnt=0; for(i=0; i<m*n; ++i) if(p[i]==i) cnt++; printf("%d\n",cnt); } return 0; }
相关文章推荐
- BZOJ 1133 [POI2009]Kon DP
- 有意练习--Rails RESTful(一)
- Linux 内核的测试和调试
- JS插件之——bootstrap-suggest.js
- 中国剩余定理
- Python的assert
- ios 爱行华夏 技术支持
- 单向电表模块焊接完成
- android 使用activity自定义下拉选择列表
- Nutch第一次搭建部署总结
- 编写一个程序,检查是否一个给定的数字是一个特殊的号码(定义数组版)
- 中国剩余定理
- easyUI之window
- String类的一些方法
- 9.17异常处理
- test4.2
- hdu1074Doing Homework(状态压缩,好题)
- 打开系统相册/相机,选择图片
- jQuery源码学习 之 立即调用 ---- day2
- Redis学习11之jedis问题之1--connect time out