【并查集】Farm Irrigation

Farm Irrigation

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
http://acm.hdu.edu.cn/showproblem.php?pid=1198
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.

【并查集】Farm Irrigation_第1张图片
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 

【并查集】Farm Irrigation_第2张图片
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


题意就是有11块拼图吧,每个都有一个河道,可以选择在拼图的中心挖井。给你一个拼好的图,问最少需要挖几个井,使得所有的水道都有水。

基础的并查集。


#include<cstdio>
using namespace std;
struct Node
{
    int up,douwn,left,right;
}mat[11]={{1,0,1,0},{1,0,0,1},{0,1,1,0},{0,1,0,1},{1,1,0,0},{0,0,1,1},{1,0,1,1},{1,1,1,0},{0,1,1,1},{1,1,0,1},{1,1,1,1}}; 
int pre[2550],num[2550];
int map[55][55];
int Find(int x)
{ 
    return pre[x]==x?x:pre[x]=Find(pre[x]);
}
void Union(int x,int y)
{
    x=Find(x);
    y=Find(y);
    if(x==y) return ;
    if(num[x]>num[y])
    {
        num[x]+=num[y];
        pre[y]=x;
    }
    else
    {
        num[y]+=num[x];
        pre[x]=y;
    }
}
int main()
{
    int n,m;
    char ch;
    for(;scanf("%d%d",&n,&m)!=EOF;)
    {
        if(n==-1&&m==-1)
            break;
        for(int i=0;i<n*m;++i) 
            pre[i]=i,num[i]=1;
        for(int i=0;i<n;++i)
        {
            getchar();
            for(int j=0;j<m;++j)
            {
                ch=getchar();
                map[i][j]=ch-'A';
            }
        }
        for(int i=0;i<n;++i) 
            for(int j=0;j<m;++j)
            {
               int x,y;
               x=i,y=j+1;
               if(y<m)
               {
                   int a=map[i][j];
                   int b=map[x][y];
                   if(mat[a].right&&mat[b].left)
                       Union(i*m+j,x*m+y);
               }
               x=i+1,y=j;
               if(x<n)
               {
                   int a=map[i][j];
                   int b=map[x][y];
                   if(mat[a].douwn&&mat[b].up)
                       Union(i*m+j,x*m+y);
               }
        }
        int cnt=0;
        for(int i=0;i<n*m;++i)
            if(pre[i]==i) 
                 ++cnt;
        printf("%d\n",cnt);
    }
    return 0;
}

来源: http://blog.csdn.net/ACM_Ted


你可能感兴趣的:(【并查集】Farm Irrigation)