hdu 1198 Farm Irrigation 并查集

来源:互联网 发布:mysql 多个字段排序 编辑:程序博客网 时间:2024/05/21 00:16
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 2DKHF3 3ADCFJKIHE-1 -1
 

Sample Output
2

3

题意:浇灌田地,图中的蓝色的线代表水管,水管能连起来的用一个泉源即可浇灌,问浇灌这片田地用需要多少个泉源,如A,可以向上方和左方连通,如B可以向上方和右方连通等等

思路:并查集,判断有多少个独立的,就需要几个泉源,例如输入a[i][j],当i不等于第一行时都可以判断和除了上方的是否连通,当i不等于最后一行时都可以判断和除了下方的是否连通,j也是如此,还有注意判断能否向右连通的同时,也要判断右边的是否能向左连通

#include<stdio.h>#include<string.h>int n,m;int k;char a[55][55];int f[2550];int t[11][4]= {{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 getf(int v){    if(f[v]==v)        return v;    else    {        f[v]=getf(f[v]);        return f[v];    }}void merge(int v,int u){    int t1,t2;    t1=getf(v);    t2=getf(u);    if(t1!=t2)    {        f[t1]=t2;    }    return ;}void fun(int x,int y){    int s1=a[x][y]-'A';    if(x!=n-1) //都可以和下方连通    {        int s2=a[x+1][y]-'A';        if(t[s1][1]==1&&t[s2][0]==1)            merge(k,k+m);    }    if(x!=0)  //都可以和上方连通    {        int s2=a[x-1][y]-'A';        if(t[s1][0]==1&&t[s2][1]==1)            merge(k,k-m);    }    if(y!=0)//都可以和左方连通    {        int s2=a[x][y-1]-'A';        if(t[s1][2]==1&&t[s2][3]==1)            merge(k,k-1);    }    if(y!=m)//都可以和右方连通    {        int s2=a[x][y+1]-'A';        if(t[s1][3]==1&&t[s2][2]==1)            merge(k,k+1);    }    k++;}int main(){    while(~scanf("%d%d",&n,&m))    {        if(n==-1&&m==-1)            break;        int i,j;        int sum=0;        for(int i=0; i<n; i++)            scanf("%s",a[i]);        for(i=1; i<=n*m; i++)            f[i]=i;        k=1;        for(i=0; i<n; i++)            for(j=0; j<m; j++)            {                fun(i,j);            }        for(int i=1; i<=n*m; i++)        {            if(f[i]==i)                sum++;        }        printf("%d\n",sum);    }    return 0;}


0 0
原创粉丝点击