HDU-1198 Farm Irrigation 并查集

Farm Irrigation

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


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

    纯模拟题啊,小心才是王道。  WA一次,就是因为把坐标转化为序号的时候把 “横坐标乘以列再加上列”  写成了 “横坐标乘以行加列” 去了。
基本思想是这样的,为每个点设置四个门,并且以 “0/1”代表该方向是否是通的,注意该题的方向一定要统一,即遍历一个点的四周所用到的dir[][]量,“门”量的上下左右对应的0,1,2,3要相同。然后更新一遍,找出根节点的个数即可。
代码如下:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

struct P
{
	int num;
	int door[4];
}p[55][55];

int dir[4][2]={ -1, 0, 1, 0, 0, 1, 0, -1 };  // 分别表示上下右左

int pipe[11][4]={ { 1, 0, 0, 1 }, { 1, 0, 1, 0 }, { 0, 1, 0, 1 }, { 0, 1, 1, 0 }, { 1, 1, 0, 0 }, { 0, 0, 1, 1 }, { 1, 0, 1, 1 }, { 1, 1, 0, 1 }, { 0, 1, 1, 1 }, { 1, 1, 1, 0 }, { 1, 1, 1, 1 } };

int set[3000], M, N;

int near( int x )
{
	return x<= 1? !x:( x> 2? 2: 3 ); 
}

int find( int x )
{
	return set[x]= x== set[x]? x: find( set[x] );
}

void merge( int x, int y )
{
	int a= find( x ), b= find( y );
	set[a]= b;
}

void update( int x, int y )
{
	for( int i= 0; i< 4; ++i )
	{
		int a= x+ dir[i][0], b= y+ dir[i][1];
		if( a< 0|| a>= M|| b< 0|| b>= N )
		{
			continue;
		}
		int num1= p[x][y]. num, num2= p[a][b]. num;
		if( find( num1 )!= find( num2 ) )
		{
			if( p[x][y]. door[i]&& p[a][b]. door[ near( i ) ] )
			{// 如果他们对应的门都是开的则将其视为一片区域
//				printf( "num1= %d, num2= %d, i= %d\n", num1, num2, i );
				merge( num1, num2 );
			}
		}
	}
}

void init( int x, int y, char op )
{
	memset( p[x][y]. door, 0, sizeof( p[x][y]. door ) );
	p[x][y]. num= x* N+ y;
	for( int i= 0; i< 4; ++i )
	{
		p[x][y]. door[i]+= pipe[ op- 'A' ][i];
//	    printf( "num[%d]= door[%d]= %d\n", p[x][y]. num, i, p[x][y]. door[i] );
	}
	
}

int main(  )
{
	while( scanf( "%d %d", &M, &N ), N>0&& M> 0 )
	{
		for( int i= 0; i< M* N; ++i )
		{
			set[i]= i;
		}
		int ans= 0;
		for( int i= 0; i< M; ++i )
		{
			char str[55], op; 
			scanf( "%s", str );
			for( int j= 0; j< N; ++j )
			{
				op= str[j];
				init( i, j, op );
			}
		}
		for( int i= 0; i< M; ++i )
		{
			for( int j= 0; j< N; ++j )
			{
				update( i, j );
			}
		}
		for( int i= 0; i< N* M; ++i )
		{
			if( set[i]== i )
			{
				ans++;
			}
		}
		printf( "%d\n", ans );
	}
}
posted @ 2011-07-19 17:00  沐阳  阅读(571)  评论(0编辑  收藏  举报