HDU-1198 Farm Irrigation(并查集)

Farm Irrigation

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


 

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

解题思路:

1.将A-K方块用结构体模拟出来,分别有参数left,right,top,bottom,参数值为true时表示可以与相应方向的方块连通。

2.利用map容器将字符和方块之间的关系映射出来

3.循环判断每个方块与之上下左右的方块是否连通,构造出连通结点数组,坐标(i,j)表示第 i*n+j+1 结点

如样例

2 2

DK

HF

连通数组应是

3 1

2 1

1 2

1 3

4.连通数组求出来后直接用并查集,AC代码如下。(不足:连通结点数组存在重复,可以优化下)

#include<iostream>
#include<map>
#include<vector>
#include<algorithm>
#include<cstring>
using namespace std;

typedef struct SQuare{
	bool left,right,bottom,top;
}Square;
const int maxn=55;
int pre[maxn*maxn],t[maxn*maxn];
int find(int x)  
{  
    int r=x;  
    while(r!=pre[r])  
        r=pre[r];  
      
    int i=x,j;  
    while(pre[i]!=r)  
    {  
        j=pre[i];  
        pre[i]=r;  
        i=j;  
    }  
    return r;  
}
void join(int x,int y)
{
	int fx=find(x),fy=find(y);
	if(fx!=fy)
		pre[fy]=fx; 
}

int main(void)
{
	map<char,Square> square;
	Square s,tmp;
	
	tmp.left=true;tmp.right=false;tmp.top=true;tmp.bottom=false;
	square.insert(pair<char,Square>('A',tmp));
	tmp.left=false;tmp.right=true;tmp.top=true;tmp.bottom=false;
	square.insert(pair<char,Square>('B',tmp));
	tmp.left=true;tmp.right=false;tmp.top=false;tmp.bottom=true;
	square.insert(pair<char,Square>('C',tmp));
	tmp.left=false;tmp.right=true;tmp.top=false;tmp.bottom=true;
	square.insert(pair<char,Square>('D',tmp));
	tmp.left=false;tmp.right=false;tmp.top=true;tmp.bottom=true;
	square.insert(pair<char,Square>('E',tmp));
	tmp.left=true;tmp.right=true;tmp.top=false;tmp.bottom=false;
	square.insert(pair<char,Square>('F',tmp));
	tmp.left=true;tmp.right=true;tmp.top=true;tmp.bottom=false;
	square.insert(pair<char,Square>('G',tmp));
	tmp.left=true;tmp.right=false;tmp.top=true;tmp.bottom=true; 
	square.insert(pair<char,Square>('H',tmp));
	
	tmp.left=true;tmp.right=true;tmp.top=false;tmp.bottom=true;
	square.insert(pair<char,Square>('I',tmp));
	
	tmp.left=false;tmp.right=true;tmp.top=true;tmp.bottom=true;
	square.insert(pair<char,Square>('J',tmp));
	tmp.left=true;tmp.right=true;tmp.top=true;tmp.bottom=true;
	square.insert(pair<char,Square>('K',tmp));

	int m,n,ans,k;
	char c[maxn][maxn];
	vector<int> res;
	
	while(cin>>m>>n && m>0 && n>0)
	{
		res.clear();
		 
		for(int i=0;i<m;i++)
			for(int j=0;j<n;j++)
				cin>>c[i][j];
		for(int i=0;i<m;i++)
		{
			for(int j=0;j<n;j++)
			{
				s=square[c[i][j]];
				
				if(i-1>=0){
					tmp=square[c[i-1][j]];
					if(s.top==true && tmp.bottom==true){
						res.push_back((i-1)*n+j+1);
						res.push_back(i*n+j+1);
					}
				}
				
				if(i+1<m){
					tmp=square[c[i+1][j]];
					if(s.bottom==true && tmp.top==true){
						res.push_back((i+1)*n+j+1);
						res.push_back(i*n+j+1);
					}
				}
				
				if(j-1>=0){
					tmp=square[c[i][j-1]];
					if(s.left==true && tmp.right==true){
						res.push_back(i*n+j);
						res.push_back(i*n+j+1);
					}
				} 
				
				if(j+1<n){
					tmp=square[c[i][j+1]];
					if(s.right==true && tmp.left==true){
						res.push_back(i*n+j+2);
						res.push_back(i*n+j+1);
					}
				} 
				
			}
		}
		for(int i=1;i<=m*n;i++)
			pre[i]=i;
		int len=res.size();
		for(int i=0;i<len;i+=2)
		{
			if(res[i]<res[i+1])
				join(res[i],res[i+1]);
			else
				join(res[i+1],res[i]);
		}
		memset(t,0,sizeof(t));
		for(int i=1;i<=m*n;i++)
			t[find(i)]=1;
		for(ans=0,k=1;k<=m*n;k++)
		{
			if(t[k])ans++;
		}
		cout<<ans<<endl;
	}
	
}

猜你喜欢

转载自blog.csdn.net/theendbigins/article/details/81133038