程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> hdu1198--並查集

hdu1198--並查集

編輯:C++入門知識

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.

 

 

\


 

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

解題思路:按行對每個節點map(i,j),找到其下方及右方的水管類型,判斷其是否能與map(i,j)相連。在這題中只需要判斷兩個方向而不是四方方向。如下

1 2 3

4 5 6

從1判斷2節點是否與1能夠相連;若相連則把1和2放入到同一個集合類中,當下次從2搜索時,由於1和2已經在一個集合類中,所以不需要對1進行搜索;如果1和2無法連在一起,那麼從2搜到1也是無法相連的。所以只需要搜索節點下方和右方的區域。

從上面給出的類型可以看出,遇到A,B,F,G這四種類型的水管時,不用搜索其下方的區域,因為無論其下方是什麼類型都不可能與這四種水管相連。同樣若是A,C,E,H這樣的類型時也不需要考慮其右邊的情況。

當前區域要想與下方的區域連在一起,則下方的水管類型只能下面的類型A,B,E,G,H,J,K。則樣若與右方區域相連,右方區域的類型為A,C,F,G,H,I,K

 

#include<stdio.h>
#include<string.h>
//
char str[2][9]={{"ABEGHJK"},{"ACFGHIK"}};
int f[2505],m,n;
char map[52][52];

int check(int x,int y)
{
	if(x>=0 && x<m && y>=0 && y<n)return 1;
	return 0;
}

int getFather(int a)
{
	while(a!=f[a])a=f[a];
	return a;
}
/** 合並兩個集合 */
void Union(int a,int b)
{
	int root1=getFather(a);
	int root2=getFather(b);
	if(root1!=root2)
	{
		if(root1<root2)f[root2]=root1;
		else f[root1]=root2;
	}
}
/** 
* 判斷兩個型號的水管是否可以連在一起 
*/
int judge(char c,int flag)
{
	int i;
	for(i=0;i<7;i++)
	{
		if(str[flag][i]==c)return i+1;
	}
	return 0;
}
void process()
{
	int i,j,dx,dy;
	char c;
	for(i=0;i<=n*m;i++)f[i]=i;
	for(i=0;i<m;i++)
	{
		for(j=0;j<n;j++)
		{
		
			c=map[i][j];
			//判斷下方,當前區域的類型是除A,B,F,G以外的。
			if(c=='C' || c=='D' || c=='E' || c=='H'
				|| c=='I' || c=='J' || c=='K')
			{
				dx = i+1;
				dy = j;
				if(check(dx,dy))
				{
					//下方區域是否是 A,B,E,G,H,J,K之一
					if(judge(map[dx][dy],0))
					{
						//合並兩個區域
						Union(i*n+j,dx*n+dy);
					}
				}
			}
			//判斷右方
			if(c=='B' || c=='D' || c=='F' || c=='G'
				|| c=='I' || c=='J' || c=='K')
			{
				dx=i;
				dy=j+1;
				if(check(dx,dy))
				{
					//右方區域是否是A,C,F,G,H,I,K之一
					if(judge(map[dx][dy],1))
					{
						Union(i*n+j,dx*n+dy);
					}
				}
			}
		}
	}
	int result=0;
	for(i=0;i<n*m;i++)if(f[i]==i)result++;
	printf("%d\n",result);
}
int main()
{
	int i;
	while(scanf("%d%d",&m,&n))
	{
		if(m<0 || n<0)break;
		for(i=0;i<m;i++)scanf("%s",map[i]);
		process();
	}
	return 0;
}

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved