杭电OJ——1198 Farm Irrigation (并查集)

Farm Irrigation


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

Author
ZHENG, Lu

Source
Zhejiang University Local Contest 2005

Recommend
Ignatius.L
这道题目有够坑的!做了我足足有三个小时!不过说实话,收获挺大的!说实话,这道题目并不难,一道典型的并查集题目,当然我也只会这一种方法,我还看见有的大神居然用搜索做了出来,够牛的!
题目咋一看,貌似摸不着头脑,不过你要想办法把问题转化到并查集上面来,我开始尝试着做的时候也是一头雾水,不过做着做着头脑就清晰了!稍微参考了一下大神的代码,发现有几点可以借鉴,在这里总结一下!
1.set数组最好还是一维数组,如果用二维的话不好控制!
2.不要怕所谓的难题,这道题目在今天以前我是想也不敢想的,但是到今天,只用了三个小时就敲出来了,因此没有所谓的难题,之所以难,是因为你水平还没有达到那一个层次,努力学习吧!多学了一点之后,你会发现,真没有那么难!
代码不做过多分析,不懂的同学可以仔细学一下并查集,很简单!可以查看这里!http://blog.csdn.net/lishuhuakai/article/details/8524906
代码如下:
#include
using namespace std;
const int MAX=60;

int set[MAX*MAX];

struct Irr
{
	int l,r,u,d;
}irr[MAX][MAX];


int findx(int x)    
{    
    int r=x;   
    while(set[r]!=r)    
        r=set[r];    
    return r;    
} 



void merge(int x,int y)    
{    
    int fx,fy;    
    fx=findx(x);    
    fy=findx(y);    
    if(fx!=fy)   
    {  
     set[fx]=fy;  
    }  
}    


int main()
{
	int line,row,cnt,a,b;
	int i,j;
	char ch;

	while(cin>>line>>row && (line!=-1 && row!=-1))
	{
		for(i=0;i>ch;
				switch(ch)
				{
				case 'A':irr[i][j].l=1;irr[i][j].r=0;irr[i][j].u=1;irr[i][j].d=0;break;
                case 'B':irr[i][j].l=0;irr[i][j].r=1;irr[i][j].u=1;irr[i][j].d=0;break;
                case 'C':irr[i][j].l=1;irr[i][j].r=0;irr[i][j].u=0;irr[i][j].d=1;break;
				case 'D':irr[i][j].l=0;irr[i][j].r=1;irr[i][j].u=0;irr[i][j].d=1;break;
				case 'E':irr[i][j].l=0;irr[i][j].r=0;irr[i][j].u=1;irr[i][j].d=1;break;
				case 'F':irr[i][j].l=1;irr[i][j].r=1;irr[i][j].u=0;irr[i][j].d=0;break;
				case 'G':irr[i][j].l=1;irr[i][j].r=1;irr[i][j].u=1;irr[i][j].d=0;break;
	     		case 'H':irr[i][j].l=1;irr[i][j].r=0;irr[i][j].u=1;irr[i][j].d=1;break;
                case 'I':irr[i][j].l=1;irr[i][j].r=1;irr[i][j].u=0;irr[i][j].d=1;break;
				case 'J':irr[i][j].l=0;irr[i][j].r=1;irr[i][j].u=1;irr[i][j].d=1;break;
				case 'K':irr[i][j].l=1;irr[i][j].r=1;irr[i][j].u=1;irr[i][j].d=1;break;
				}
			}

           for(i=0;i=0 )
					   if(irr[a][b].d==1 && irr[i][j].u==1)
						   merge(i*row+j,a*row+b);

				   a=i+1;b=j;
				   if(a=0)
					   if(irr[a][b].r==1 && irr[i][j].l==1)
						   merge(i*row+j,a*row+b);

				   a=i;b=j+1;
				   if(b
using namespace std;
#define MAXN 505

struct Farm {
    bool up;
    bool down;
    bool left;
    bool right;
}farms[MAXN];

int father[MAXN * MAXN];//储存根部元素

int findSet(int a)//查找根部元素
{
    if (a==father[a])
        return a;
	else
    return findSet(father[a]);

}
void Union(int a,int b)
{
    int x=findSet(a);
    int y=findSet(b);
    if (x==y)
	{
        return;
    }
    father[y]=x;
    return;
}//两个集合的合并

int findRoot(int n)//求一共有多少个集合
{
    int count=0;
    for (int i=0; i>n>>m)
	{
        if(n==-1 && m==-1) break;
           
        init();
        for (int i=0; i>map[i][j];
            }
        }
        for (int i=0; i=0 )
				{
                    if (farms[map[i-1][j]-'A'].down && farms[map[i][j]-'A'].up)
					{ 
                        int x=i*m +j;
                        int y=(i-1)*m+j;
                        Union(x,y);
                    } 
                }
                if ((i + 1)= 0) {
                    if (farms[map[i][j-1]-'A'].right && farms[map[i][j]-'A'].left) {
                        int x = i * m + j;
                        int y = i * m + (j - 1);
                        Union(x, y);
                    } 
                }
                if ((j + 1) < m) {
                    if (farms[map[i][j+1]-'A'].left && farms[map[i][j]-'A'].right) {
                        int x = i * m + j;
                        int y = i * m + (j + 1);
                        Union(x, y);
                    } 
                }
            }
        }
        cout<


你可能感兴趣的:(杭电OJ——1198 Farm Irrigation (并查集))