hduoj1198(并查集)
来源:互联网 发布:淘宝客qq群现在好做吗 编辑:程序博客网 时间:2024/06/04 18:55
Farm Irrigation
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 8591 Accepted Submission(s): 3691
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
2 2
DK
HF
3 3
ADC
FJK
IHE
-1 -1
Sample Output
2
3
Author
ZHENG, Lu
题解:将农场中的方块从0到m*n进行编号,然后每一个方块与相邻的右边和下面的方块进行判定是否联通,如果联通则将两个方块放到一个集合中,对计数器减一,最后输出计数器也就是联通的集合数。
#include<iostream>#include<cstdio>using namespace std;int map[555][555];int n,m;int pre[555*555];int cnt;int linked[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} };void makeset(){ int i; for(i=0;i<m*n;i++){ pre[i]=i; } cnt=m*n; return;}int find(int x){ if(x!=pre[x]){ pre[x]=find(pre[x]); } return pre[x];} void uniun(int a,int b){ if(find(b)!=find(a)){ pre[find(b)]=find(a); cnt--; }}void count(int x1,int y1,int x2,int y2,bool r){ if(x2>m-1||y2>n-1)return; bool mark=false; //cout<<mark<<endl; if(r){ if(linked[map[x1][y1]][1]&&linked[map[x2][y2]][3])mark=true; } else{ if(linked[map[x1][y1]][2]&&linked[map[x2][y2]][0])mark=true; } //cout<<mark<<endl; if(mark){ uniun(x1*n+y1,x2*n+y2); //uniun((x1-1)*m+y1,(x2-1)*m+y2); //printf("%d\n",cnt); //cnt--; } return; }int main(){ int i,j; char c; while(scanf("%d%d",&m,&n)){ if(n==-1&&m==-1)break; makeset(); for(i=0;i<m;i++) for(j=0;j<n;j++){ cin>>c; map[i][j]=c-'A'; } for(i=0;i<m;i++) for(j=0;j<n;j++){ //printf("map=%d ",map[i][j]); count(i,j,i+1,j,false); count(i,j,i,j+1,true); } printf("%d\n",cnt); } return 0;}
- hduoj1198(并查集)
- HDU3938 并查集 并查集
- 并查集(集并查)
- HDU1232 并查集<并>
- 并查集
- 数据结构-并查集
- 并查集
- 并查集!
- 并查集
- 并查集
- 并查集
- 并查集
- 并查集总结
- 并查集学习
- 并查集
- 并查集
- 并查集
- 所谓并查集
- Using convolutional neural nets to detect facial keypoints tutorial
- Android 实现简易下载管理器 (暂停、断点续传、多线程下载)
- IOS 解决集成zbar链接错误
- windows下安装nginx
- com.android.build.transform.api.TransformException
- hduoj1198(并查集)
- ios 指定某个页面是横屏还是竖屏
- Android Studio Mac版快捷键
- iOS沙盒目录结构解析
- BLOB和TEXT的区别
- 修改mysql编码格式,存入中文变为问号
- Ubuntu下LTE的加载过程
- GROUP BY ORDER BY 同时使用按时间排序
- Android布局