码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 1198 并查集

时间:2014-07-18 10:03:25      阅读:254      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   java   color   

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.

bubuko.com,布布扣

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 

bubuko.com,布布扣

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
 
 
题目意思:
一块矩形土地分为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 }

 

 

HDU 1198 并查集,布布扣,bubuko.com

HDU 1198 并查集

标签:des   style   blog   http   java   color   

原文地址:http://www.cnblogs.com/qq1012662902/p/3852411.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!