J - Farm Irrigation HDU - 1198(并查集)

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

题意:
有A-K命名的多个方块,每个方块有可以联通的方向,输入M行N列的字符矩阵,让你求出有多少个联通的方块
解法:
并查集模板加上A-K方块的特殊初始化,然后依次遍历字符矩阵左右和上下是否联通,用标记数组来查看有多少个联通块。

AC代码

#include<iostream>
#include<string.h>
#include<stdio.h>
#include<math.h>
#include<set>
#include<algorithm>
#define INF 0x3f3f3f3f
using namespace std;
int pre[2550],vst[2550];
char map[55][55];
struct P
{
	int up,down,left,right;	
}p[15];//记录A-K方块的联通方向
void Pinit(int v,int a,int b,int c,int d)
{
	p[v].up=a;
	p[v].down=b;
	p[v].left=c;
	p[v].right=d;
}
void make()
{
	for(int i=0;i<2550;i++)
	{
		pre[i]=i;
		vst[i]=0;
	}
	for(int i=0;i<11;i++)//每个小方块的初始化
	{
		switch(i)
		{
			case 0:Pinit(i,1,0,1,0);break;
			case 1:Pinit(i,1,0,0,1);break;
			case 2:Pinit(i,0,1,1,0);break;
			case 3:Pinit(i,0,1,0,1);break;
			case 4:Pinit(i,1,1,0,0);break;
			case 5:Pinit(i,0,0,1,1);break;
			case 6:Pinit(i,1,0,1,1);break;
			case 7:Pinit(i,1,1,1,0);break;
			case 8:Pinit(i,0,1,1,1);break;
			case 9:Pinit(i,1,1,0,1);break;
			case 10:Pinit(i,1,1,1,1);break;
		}
	}
}
int check(int i,int j)
{
	if(p[i].right==1&&p[j].left==1)
	return 1;
	return 0;
}
int check1(int i,int j)
{
	if(p[i].down==1&&p[j].up==1)
	return 1;
	return 0;
}
int find(int i)
{
	return pre[i]==i?i:pre[i]=find(pre[i]);
}
void unite(int i,int j)
{
	i=find(i);
	j=find(j);
	if(i!=j)
	{
		pre[i]=j;
	}
}
int main()
{
	int m,n;
	while(~scanf("%d %d",&m,&n))
	{
		memset(map,0,sizeof(map));
		if(m<0||n<0)
		{
			break;
		}
		make();
		for(int i=0;i<m;i++)
		{
			scanf("%s",map[i]);
		}
		int num=1;//用来记录到第几个小方块了
		for(int i=0;i<m;i++)
		{
			for(int j=0;j<n;j++)
			{
				if(j!=n-1)
				{
					if(check(map[i][j]-'A',map[i][j+1]-'A'))//检查左右是否联通
					{
						unite(num,num+1);
					}
				}
				if(i!=m-1)
				{
					if(check1(map[i][j]-'A',map[i+1][j]-'A'))//检查上下是否联通
					{
						unite(num,num+n);
					}
				}
				num++;
			}
		}
		int sum=0;
		for(int i=1;i<num;i++)
		{
			int x=find(i);
			if(vst[x]==0)//标记数组做判断
			{
				sum++;
				vst[x]=1;
			}
		}
		cout<<sum<<endl;
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值