HDU 题目1198 Farm Irrigation 并查集
来源:互联网 发布:诺基亚n8刷windows 编辑:程序博客网 时间:2024/06/05 15:43
Farm Irrigation
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 10097 Accepted Submission(s): 4450
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.
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
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
简单的并查集 加 搜索思路去解题。
#include<iostream>#include<algorithm>#include<cstdio>#include<cstring>#include<string>using namespace std;#define maxx 60*60struct directiono{ int upper,lower,left,right;}direction[60][60];//定义每个块的四个方向的状态int text[maxx];int n,m;int getfather(int x)//寻找父节点{ int tt=x; while(tt!=text[tt]) tt=text[tt]; return tt;}void father(int x,int y)//判断是否在同一集合 { int xx,yy; xx=getfather(x); yy=getfather(y); if(xx!=yy) text[xx]=yy;}int main(){ char te; while(scanf("%d%d",&n,&m)!=EOF) { if(n==-1&&m==-1) break; for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { cin>>te; switch(te)//对 输入字母的上下左右的状态进行记录 { case 'A': direction[i][j].upper=1;direction[i][j].lower=0;direction[i][j].left=1;direction[i][j].right=0; break; case 'B': direction[i][j].upper=1;direction[i][j].lower=0;direction[i][j].left=0;direction[i][j].right=1; break; case 'C': direction[i][j].upper=0;direction[i][j].lower=1;direction[i][j].left=1;direction[i][j].right=0; break; case 'D': direction[i][j].upper=0;direction[i][j].lower=1;direction[i][j].left=0;direction[i][j].right=1; break; case 'E': direction[i][j].upper=1;direction[i][j].lower=1;direction[i][j].left=0;direction[i][j].right=0; break; case 'F': direction[i][j].upper=0;direction[i][j].lower=0;direction[i][j].left=1;direction[i][j].right=1; break; case 'G': direction[i][j].upper=1;direction[i][j].lower=0;direction[i][j].left=1;direction[i][j].right=1; break; case 'H': direction[i][j].upper=1;direction[i][j].lower=1;direction[i][j].left=1;direction[i][j].right=0; break; case 'I': direction[i][j].upper=0;direction[i][j].lower=1;direction[i][j].left=1;direction[i][j].right=1; break; case 'J': direction[i][j].upper=1;direction[i][j].lower=1;direction[i][j].left=0;direction[i][j].right=1; break; case 'K': direction[i][j].upper=1;direction[i][j].lower=1;direction[i][j].left=1;direction[i][j].right=1; break; } } } for(int i=0;i<=n*m;i++)//对 每个点进行 初始化 每个点的根都是它本身 text[i]=i; for(int i=0;i<n;i++) { for(int j=0;j<m;j++)//每个点向上下左右四个方向进行并查集 { if(i>=1) { if(direction[i-1][j].lower==1&&direction[i][j].upper==1) father(i*m+j,(i-1)*m+j); } if(i+1<n) { if(direction[i][j].lower==1&&direction[i+1][j].upper==1) father(i*m+j,(i+1)*m+j); } if(j>=1) { if(direction[i][j].left==1&&direction[i][j-1].right==1) father(i*m+j,i*m+j-1); } if(j+1<m) { if(direction[i][j].right==1&&direction[i][j+1].left==1) father(i*m+j,i*m+j+1); } } } int num=0; for(int i=0;i<n*m;i++)//统计根的个数 if(i==text[i]) num++; cout<<num<<endl; } return 0;}
- HDU 题目1198 Farm Irrigation 并查集
- hdu 1198 Farm Irrigation(并查集)
- hdu 1198 Farm Irrigation( 并查集)
- HDU 1198 Farm Irrigation(并查集)
- HDU 1198 Farm Irrigation 【并查集】
- HDU 1198Farm 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 并查集+枚举
- redis模糊查询scan
- XSL常见问题及解决(二)有关Xpath
- 隐马尔科夫模型(HMM)
- windows下的navicat的安装、激活、汉化
- ELF目标文件结构
- HDU 题目1198 Farm Irrigation 并查集
- 2--理解并学习javascript中的面向对象(OOP)(续)
- 微机原理期中考试
- tortoisegit使用教程 --- 修改提交简易图文教程
- 算法概论 习题8.20
- jq insertBefore 的返回值
- tomcat版本换了报错:报错:The Apache Tomcat installation at this directory is version 8.5.6. A Tomcat 8.0 ins
- 今天研究jquery源码的收获
- 流媒体基本要点简述:如何在H264数据中获取PTS?