HDU-1198 并查集

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<bits/stdc++.h>
using namespace std;
int pre[2501];
int find(int a){return pre[a]==-1?a:pre[a]=find(pre[a]);}
void merge(int a,int b){
	a=find(a),b=find(b);
	if(a!=b)pre[a]=b;
}
struct point{
	int l,r,u,d;
}p[52][52];
int f[4][2]={1,0,0,1,-1,0,0,-1},id[52][52];
int main(){
	point A={1,0,1,0},B={0,1,1,0},C={1,0,0,1},D={0,1,0,1},E={0,0,1,1};
	point F={1,1,0,0},G={1,1,1,0},H={1,0,1,1},I={1,1,0,1},J={0,1,1,1},K={1,1,1,1};
	int n,m;
	while(~scanf("%d%d",&n,&m)){
		if(n==-1&&m==-1)break;
		memset(pre,-1,sizeof(pre));
		int cnt=0;
		for(int i=0;i<n;++i)
			for(int j=0;j<m;++j){
				char c;cin>>c;
				switch(c){
					case 'A':p[i][j]=A;break;
					case 'B':p[i][j]=B;break;
					case 'C':p[i][j]=C;break;
					case 'D':p[i][j]=D;break;
					case 'E':p[i][j]=E;break;
					case 'F':p[i][j]=F;break;
					case 'G':p[i][j]=G;break;
					case 'H':p[i][j]=H;break;
					case 'I':p[i][j]=I;break;
					case 'J':p[i][j]=J;break;
					case 'K':p[i][j]=K;break;
				}
				id[i][j]=cnt++;
			}
		for(int i=0;i<n;++i)
			for(int j=0;j<m;++j)
				for(int k=0;k<4;++k){
					int x=i+f[k][0],y=j+f[k][1];
					if(x<0||y<0||x>=n||y>=m)continue;
					switch(k){
						case 0:if(p[i][j].d+p[x][y].u==2)merge(id[i][j],id[x][y]);break;
						case 1:if(p[i][j].r+p[x][y].l==2)merge(id[i][j],id[x][y]);break;
						case 2:if(p[i][j].u+p[x][y].d==2)merge(id[i][j],id[x][y]);break;
						case 3:if(p[i][j].l+p[x][y].r==2)merge(id[i][j],id[x][y]);break;
					}

				}
		int ans=0;
		for(int i=0;i<cnt;++i)if(find(i)==i)++ans;
		printf("%d\n",ans);
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值