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

HDU 1198-Farm Irrigation(并查集)

时间:2014-08-15 16:03:28      阅读:209      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   color   java   os   io   

Farm Irrigation

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5820    Accepted Submission(s): 2523


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
很坑爹的并查集,输入以字母给出,所以对于每一个字母,都要判断它是否和上下左右的字母连通,如果连通,则将它们合并,最后集合的个数就是答案。很无奈我只好在纸上把和所有相连通的字母找出来然后存在了数组里面。
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int m,n,fa[10010],hash[10010];
char ma[60][60];
void Make_set(int n)
{
	for(int i=1;i<=n;i++)
	fa[i]=i;
}
int Find(int x)
{
    if(x!=fa[x])
		fa[x]=Find(fa[x]);
	return fa[x];
}
void Union(int x,int y)
{
	int fx=Find(x);
	int fy=Find(y);
	if(fx==fy)
		return ;
	fa[fy]=fx;
}
int dir[4][2]={{-1,0},{1,0},{0,-1},{0,1}};
char ok[11][4][12]={{"CDEHIJK","","BDFGIJK",""},{"CDEHIJK","","","ACFGHIK"},{"","ABEGHJK","BDFGIJK",""},{"","ABEGHJK","","ACFGHIK"},{"CDEHIJK","ABEGHJK","",""},{"","","BDFGIJK","ACFGHIK"},{"CDEHIJK","","BDFGIJK","ACFGHIK"},{"CDEHIJK","ABEGHJK","BDFGIJK",""},{"","ABEGHJK","BDFGIJK","ACFGHIK"},{"CDEHIJK","ABEGHJK","","ACFGHIK"},{"CDEHIJK","ABEGHJK","BDFGIJK","ACFGHIK"}};
void bfs(int x,int y)
{
  	int i,tx=x,ty=y;
  	for(i=0;i<4;i++)
	{
		tx=x+dir[i][0];
		ty=y+dir[i][1];
		if(0<=tx&&tx<m&&0<=ty&&ty<n)
		{
			int flag=0,len=strlen(ok[ma[x][y]-'A'][i]);
            for(int j=0;j<len;j++)
			{
				if(ok[ma[x][y]-'A'][i][j]==ma[tx][ty])
				{
					flag=1;
					break;
				}
			}
			if(flag)
				Union(x*n+y+1,tx*n+ty+1);
		}
	}
}
int main()
{
	int i,j;
	while(~scanf("%d%d",&m,&n))
	{
		memset(hash,0,sizeof(hash));
		if(m==-1&&n==-1) break;
		for(i=0;i<m;i++)
			scanf("%s",ma[i]);
		Make_set(m*n);
		for(i=0;i<m;i++)
			for(j=0;j<n;j++)
			bfs(i,j);
		for(i=1;i<=m*n;i++)
		{
			int f=Find(i);
			hash[f]++;
		}
		int cnt=0;
		for(i=1;i<=m*n;i++)
			if(hash[i])
				 cnt++;
		printf("%d\n",cnt);
	}
	return 0;
}


HDU 1198-Farm Irrigation(并查集),布布扣,bubuko.com

HDU 1198-Farm Irrigation(并查集)

标签:des   style   blog   http   color   java   os   io   

原文地址:http://blog.csdn.net/qq_16255321/article/details/38585751

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