hdu1198 并查集

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

 

思路:从上往下 从左往右 判断 是否可以与右边 下边 连接  如果可以,那么并一起

   最后输出集合的个数

 

 

思路2:暴力dfs 也可以 即求不连通的数量

 

 

#include <bits/stdc++.h>
using namespace std;
const int  maxn=1e4+5;
int pre[maxn]; //父节点 
//int height[maxn]; //树的高度 
//int sum[maxn]; //节点的数量 
//int cnt[maxn]; //移动次数; 
char ma[55][55];
bool circle=false;	
int m,n;
char str[2][9]={{"ABEGHJK"},{"ACFGHIK"}};
void init(){
	for(int i=0;i<maxn;i++){
		pre[i]=i;
		//height[i]=0; //树的高度
		//sum[i]=1;
		//cnt[i]=0; 
	}
}
int find(int x)   //合并路径 找根  带权 
{  
    if(pre[x]==x)  
        return pre[x];  
    else  
    {  
    	int temp=pre[x];
        int root = find(pre[x]);  
        //cnt[x] += cnt[temp];
        return pre[x] = root;  
    }  
}  
int find_set(int x){    //非递归实现 
	if(pre[x]==x) return x;
	else {
		int root,son,tmp;
		son=x;
		root=x;
		while(pre[root]!=root) {   
			root=pre[root];
		}
		while(son!=root){
			tmp=pre[son];
			pre[son]=root;
			son=tmp;
		}
		return root;
	}
}
void union_set(int x,int y){
	x=find_set(x);
	y=find_set(y);
	if(x!=y){
		pre[x]=y;
		//sum[y]+=sum[x];
	}else{
		circle=true;
	}
}
int union_set2(int x, int y){
	x=find(x);
	y=find(y);
	if(x!=y){
		pre[x]=y;
		//sum[y]+=sum[x];
		//cnt[x]=1;
		//sum[x]=0;
	}
}
int check(int x,int y)
{
    if(x>=0 && x<m && y>=0 && y<n)return 1;
    return 0;
}
/** 
* 判断两个型号的水管是否可以连在一起 
*/
int judge(char c,int flag)
{
    int i;
    for(i=0;i<7;i++)
    {
        if(str[flag][i]==c)return i+1;
    }
    return 0;
}
int main(){

	while(scanf("%d%d",&m,&n)){
		if(m+n<0) break;	
		for(int i=0;i<m;i++){
			scanf("%s",ma[i]);
		}
		init();
		for(int i=0;i<m;i++){
	        for(int j=0;j<n;j++){
	            char c=ma[i][j];
	            //判断下方,当前区域的类型是除A,B,F,G以外的。
	            if(c=='C' || c=='D' || c=='E' || c=='H'
	                || c=='I' || c=='J' || c=='K'){
	                int dx = i+1;
	                int dy = j;
	                if(check(dx,dy))
	                {
	                    //下方区域是否是 A,B,E,G,H,J,K之一
	                    if(judge(ma[dx][dy],0))
	                    {
	                        //合并两个区域
	                        union_set(i*n+j,dx*n+dy);
	                    }
	                }
	            }
	            //判断右方
	            if(c=='B' || c=='D' || c=='F' || c=='G'
	                || c=='I' || c=='J' || c=='K')
	            {
	                int dx=i;
	                int dy=j+1;
	                if(check(dx,dy))
	                {
	                    //右方区域是否是A,C,F,G,H,I,K之一
	                    if(judge(ma[dx][dy],1))
	                    {
	                        union_set(i*n+j,dx*n+dy);
	                    }
	                }
	            }
        	}
    	}
    	int result=0;
    	for(int i=0;i<n*m;i++)if(pre[i]==i)result++;
    	printf("%d\n",result);
	}
	return 0;
}

  

posted @ 2019-11-20 11:19  杰瑞与汤姆  阅读(125)  评论(0编辑  收藏  举报