杭电oj1198:Farm Irrigation(并查集)

Farm Irrigation

题目链接

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)

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.
杭电oj1198:Farm Irrigation(并查集)_第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
杭电oj1198:Farm Irrigation(并查集)_第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

刚一开始看到这道题目的时候,一点思路也没有,甚至连怎么存都不知道,随着思考的深入,清晰的思路也慢慢的浮出水面。
合并思路如图所示:
杭电oj1198:Farm Irrigation(并查集)_第3张图片
注意:最后一列和最后一行需要特别处理。

#include
using namespace std;

const int maxn = 55;

//依次存放题目中的 A~K 
int map[11][4] = {
     {
     1,0,1,0},{
     0,1,1,0},{
     1,0,0,1},{
     0,1,0,1},
					{
     0,0,1,1},{
     1,1,0,0},{
     1,1,1,0},{
     1,0,1,1},
					{
     1,1,0,1},{
     0,1,1,1},{
     1,1,1,1}};

int father[maxn*maxn];//存放父亲节点
bool isRoot[maxn*maxn];//记录某个节点是否作为某个集合的根节点

//存放数据的结构体数组
struct STR
{
     
	int left;
	int right;
	int up;
	int down;
}str[maxn][maxn];

void init(int n)//初始化 
{
     
	for(int i=1;i<=n;i++)
	{
     
		father[i] = i;
		isRoot[i] = false;
	}
}

//查找x所在集合的根节点
int findFather(int x)
{
     
	int a = x;
	while(x != father[x])
		x = father[x];
	
	//路径压缩 
	while(a != father[a])
	{
     
		int z = a;
		a = father[a];
		father[z] = x;
	}
	return x;
}

void Union(int a, int b)//合并a和b所在的集合
{
     
	int faA = findFather(a);
	int faB = findFather(b);
	if(faA != faB)
		father[faA] = faB;
} 

int main()
{
     
	//freopen("in.txt","r", stdin);
	int m,n;
	while(cin>>m>>n, m>0 && n>0)
	{
     
		char c;
		for(int i=1;i<=m;i++)
		{
     
			for(int j=1;j<=n;j++)
			{
     
				cin>>c;
				//给结构体赋值
				str[i][j].left = map[c-'A'][0];
				str[i][j].right = map[c-'A'][1];
				str[i][j].up = map[c-'A'][2];
				str[i][j].down = map[c-'A'][3];
			}
		}
		
		init(m*n);//初始化 
		
		for(int i=1;i<=m;i++)
		{
     
			for(int j=1;j<=n;j++)
			{
     
				//最后一块区域不需要做处理,前边已经处理完成
				if(i==m && j==n)
					continue;
				//最后一列
				if(j==n)
				{
     
					if(str[i][j].down == str[i+1][j].up && str[i][j].down == 1)
						Union((i-1)*n+j, i*n+j);
					continue;
				}
				//最后一行
				if(i==m)
				{
     
					if(str[i][j].right == str[i][j+1].left && str[i][j].right == 1)
						Union((i-1)*n+j, (i-1)*n+j+1);
					continue;
				}
				//检测str[i][j]与str[i][j+1]是否相连
				if(str[i][j].right == str[i][j+1].left && str[i][j].right == 1)
					Union((i-1)*n+j, (i-1)*n+j+1);
				
				//检测str[i][j]与str[i+1][j]是否相连
				if(str[i][j].down == str[i+1][j].up && str[i][j].down == 1)
					Union((i-1)*n+j, i*n+j);
			}
		}
		
		for(int i=1;i<=m*n;i++)
			isRoot[findFather(i)] = true;
		
		int ans = 0;
		for(int i=1;i<=m*n;i++)
			ans += isRoot[i];
		
		cout<<ans<<endl;
	}
	return 0;
}

注意:如果处理合并的代码写成如下形式是错误的。

for(int i=1;i<=m;i++)
		{
     
			for(int j=1;j<=n;j++)
			{
     
				if(i==m && j==n)
					continue;
/************************错误之处************************/
				if(j==n && str[i][j].down == str[i+1][j].up && str[i][j].down == 1)
				{
     
					Union((i-1)*n+j, i*n+j);
					continue;
				}
				if(i==m && str[i][j].right == str[i][j+1].left && str[i][j].right == 1)
				{
     
					Union((i-1)*n+j, (i-1)*n+j+1);
					continue;
				}
/*********************************************************/
				//检测str[i][j]与str[i][j+1]是否相连
				if(str[i][j].right == str[i][j+1].left && str[i][j].right == 1)
					Union((i-1)*n+j, (i-1)*n+j+1);
				
				//检测str[i][j]与str[i+1][j]是否相连
				if(str[i][j].down == str[i+1][j].up && str[i][j].down == 1)
					Union((i-1)*n+j, i*n+j);
			}
		}

如果想不出来为什么错误,把下边的例子执行一遍就知道啦
1 5
FFEEJ

你可能感兴趣的:(并查集,杭电oj,oj系统,数据结构,算法)