并查集

F - Farm Irrigation
Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

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
 
  1. #include<cstdio>  
  2. #include<cstring>  
  3. #include<iostream>  
  4. #include<algorithm>  
  5. using namespace std;  
  6.   
  7. const int N = 55;  
  8. const int MAXN = 10000;  
  9.   
  10. int n , m;  
  11. int father[MAXN];  
  12. char mat[N][N];  
  13. int state[11][4] =   
  14. {{1,0,0,1},{1,1,0,0},{0,0,1,1},{0,1,1,0},{1,0,1,0},{0,1,0,1},  
  15.     {1,1,0,1},{1,0,1,1},{0,1,1,1},{1,1,1,0},{1,1,1,1}};  
  16.   
  17. void init(){  
  18.     for(int i = 0 ; i < MAXN ; i++)  
  19.         father[i] = i;  
  20. }  
  21.   
  22. int find(int x){  
  23.     if(father[x] != x)  
  24.         father[x] = find(father[x]);  
  25.     return father[x];  
  26. }  
  27.   
  28. void Union(int x , int y){  
  29.     int fx = find(x);      
  30.     int fy = find(y);      
  31.     father[fx] = fy;  
  32. }  
  33.   
  34. void output(){  
  35.     set<int> s;  
  36.     for(int i = 0 ; i < n ; i++)  
  37.         for(int j =  0 ; j < m ; j++)  
  38.             s.insert(find(i*m+j));  
  39.     printf("%d\n" , s.size());  
  40. }  
  41.   
  42. int main(){  
  43.     char ch;  
  44.     while(scanf("%d%d%*c" , &n , &m) && n > 0){  
  45.         init();     
  46.         for(int i = 0 ; i < n ; i++){  
  47.             for(int j =  0 ; j < m ; j++){  
  48.                 scanf("%c" , &mat[i][j]);   
  49.                 ch = mat[i][j];  
  50.               <span style="color:#ff0000;">  int x = ch-'A';  
  51.                 if(i && state[x][0] == 1){  
  52.                    int y = mat[i-1][j]-'A';   
  53.                    if(state[x][0] == state[y][2])  
  54.                       Union(i*m+j , (i-1)*m+j);  
  55.                 }  
  56.                 if(j && state[x][3] == 1){  
  57.                    int y = mat[i][j-1]-'A';   
  58.                    if(state[x][3] == state[y][1])  
  59.                       Union(i*m+j , i*m+j-1);  
  60.                 }</span>  
  61.             }   
  62.             getchar();  
  63.         }  
  64.         output();  
  65.     }  
  66.     return 0;  
  67. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
并查集(Disjoint Set)是一种数据结构,用于解决集合的合并和查找问题。在Python中可以使用类来实现并查集。引用展示了一个简单的并查集类的代码实现,其中包括了初始化集合、查找集合、合并集合和判断两个元素是否在同一个集合中的方法。另外,引用和展示了对并查集代码的优化,包括路径压缩和按秩合并等技巧,以提高并查集的效率。 在Python中使用并查集可以解决一些实际问题,如求解岛屿个数、朋友圈等。通过将问题转化为集合的合并和查找操作,可以使用并查集来高效地解决这些问题。 所以,如果你需要在Python中实现并查集,可以参考以上的代码实现和优化方法。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [python 数据结构与算法——并查集](https://blog.csdn.net/itnerd/article/details/103916115)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [并查集Python版](https://blog.csdn.net/XZ2585458279/article/details/127274576)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值