首页 > 代码库 > HDU 1198 并查集
HDU 1198 并查集
Farm Irrigation
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5444 Accepted Submission(s): 2341
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.
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
题目意思:
一块矩形土地分为n*m个小矩形,有11种水管分别用A~K表示,给出土地上水管铺设情况,求最少设置多少个水源使得土地全部能被灌溉。
思路:
每个小矩阵可以用数字表示为1~n*m,从左上角开始看右边和上边土地的水管能否和此处土地上的水管相连,若能则并在一个集合里。最后输出father[i]=i的数目即为答案。
代码:
1 #include <cstdio> 2 #include <cstring> 3 #include <string> 4 #include <algorithm> 5 #include <iostream> 6 using namespace std; 7 8 int father[55*55]; 9 char map[55][55];10 11 int findroot(int p){12 int r=p;13 while(r!=father[r]) r=father[r];14 int x, j;15 x=p;16 while(x!=r){17 j=father[x];18 father[x]=r;19 x=j;20 }21 return r;22 }23 24 25 26 27 main()28 {29 int n, m, i, j, k;30 while(scanf("%d %d",&n,&m)==2&&n!=-1){31 for(i=0;i<n;i++) scanf("%s",map[i]);32 for(i=0;i<=n*m+1;i++) father[i]=i;33 for(i=0;i<n;i++){34 for(j=0;j<m;j++){35 if(map[i][j]==‘A‘||map[i][j]==‘B‘||map[i][j]==‘E‘||map[i][j]==‘G‘||map[i][j]==‘H‘||map[i][j]==‘J‘||map[i][j]==‘K‘)36 {37 if(i>0&&(map[i-1][j]==‘C‘||map[i-1][j]==‘D‘||map[i-1][j]==‘E‘||map[i-1][j]==‘H‘||map[i-1][j]==‘I‘||map[i-1][j]==‘J‘||map[i-1][j]==‘K‘))38 { //向上 39 int fx=findroot(i*m+j);40 int fy=findroot((i-1)*m+j);41 if(fx!=fy) father[fy]=fx;42 }43 }44 if(map[i][j]==‘B‘||map[i][j]==‘D‘||map[i][j]==‘F‘||map[i][j]==‘G‘||map[i][j]==‘I‘||map[i][j]==‘J‘||map[i][j]==‘K‘)45 {46 if(j<m-1&&(map[i][j+1]==‘A‘||map[i][j+1]==‘C‘||map[i][j+1]==‘F‘||map[i][j+1]==‘G‘||map[i][j+1]==‘H‘||map[i][j+1]==‘I‘||map[i][j+1]==‘K‘))47 { //向右 48 int fx=findroot(i*m+j);49 int fy=findroot(i*m+j+1);50 if(fx!=fy) father[fy]=fx;51 }52 }53 54 55 }56 }57 int num=0;58 for(i=0;i<n*m;i++)59 if(father[i]==i) num++;60 // printf("%d ",father[i]);61 printf("%d",num);62 printf("\n");63 64 }65 }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。