杭电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

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

		   for(i=0;i<line;i++)
			   for(j=0;j<row;j++)
			   {
				   a=i-1;b=j;
				   if(a>=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<line)
					   if(irr[a][b].u==1 && irr[i][j].d==1)
						   merge(i*row+j,a*row+b);

				   a=i;b=j-1;
				   if(b>=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<row)
					   if(irr[a][b].l==1 && irr[i][j].r==1)
						   merge(i*row+j,a*row+b);
			   }
			   cnt=0;
			   for(i=0;i<line*row;i++)
					  if(set[i]==i)
						  cnt++;
			   cout<<cnt<<endl;


	}
}

/*
#include<iostream>
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; i++)
	{
        if (father[i] == i)
		{
            count ++;
        }
    }
    return count;
}

void init()//初始化
{
    for(int i=0; i<MAXN*MAXN; i++) 
     father[i] = i;


    farms[0].up = 1;
    farms[0].left = 1;

    farms[1].up = 1;
    farms[1].right = 1;

    farms[2].left = 1;
    farms[2].down = 1;

    farms[3].right = 1;
    farms[3].down = 1;

    farms[4].up = 1;
    farms[4].down = 1;

    farms[5].left = 1;
    farms[5].right = 1;

    farms[6].left = 1;
    farms[6].up = 1;
    farms[6].right = 1;

    farms[7].left = 1;
    farms[7].up = 1;
    farms[7].down = 1;

    farms[8].left = 1;
    farms[8].down = 1;
    farms[8].right = 1;

    farms[9].up = 1;
    farms[9].right = 1;
    farms[9].down = 1;

    farms[10].left = 1;
    farms[10].right = 1;
    farms[10].up = 1;
    farms[10].down = 1;
}
char map[MAXN][MAXN];

int main() 
{
    int n, m;
    while (cin>>n>>m)
	{
        if(n==-1 && m==-1) break;
           
        init();
        for (int i=0; i<n; i++)
		{
            for (int j=0; j<m; j++)
			{
                cin>>map[i][j];
            }
        }
        for (int i=0; i<n; i++)
		{
            for (int j=0; j<m; j++)
			{
                if ( (i-1)>=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)<n) 
				{
                    if (farms[map[i+1][j]-'A'].up && farms[map[i][j]-'A'].down)
					{
                        int x = i * m + j;
                        int y = (i + 1) * m + j;
                        Union(x, y);
                    } 
                }
                if ((j - 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<<findRoot(n*m)<<endl;

    }
}
*/


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值