hdu 1198 Farm Irrigation

Farm Irrigation

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


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

题目大意。有很多水管。由题目中给出。问至少需要多少水源才能保证整个田地都能灌溉到。
#include <stdio.h>

int mem[10000];//用于建立并查集
int msize[10000];//记录集合中水管数
int mway[10000][5];//记录每块田里的水管类型
int sum;//记录集合数目。即所需水源数

int finder(int x)//找根节点
{
    int i,j,t;
    i=x;
    while(mem[i]!=i)
        i=mem[i];
    j=x;
    while(j!=i)
    {
        t=mem[j];
        mem[j]=i;
        j=t;
    }
    return i;
}
void toone(int a,int b)//加入集合
{
    int r1,r2;
    //printf("a:%d b:%d\n",a,b);
    r1=finder(a);
    r2=finder(b);
    //printf("r1:%d r2:%d\n",r1,r2);
    if(r1==r2)
        return ;
    if(msize[r1]>msize[r2])
    {
        mem[r2]=r1;
        msize[r1]+=msize[r2];
    }
    else
    {
        mem[r1]=r2;
        msize[r2]+=msize[r1];
    }
    sum--;
}
int main()
{
    int i,j,t,m,n;
    char c;

    while(scanf("%d%d",&m,&n)!=EOF)
    {
        getchar();
        if(m==-1&&n==-1)
            break;
        sum=m*n;//sum初始为合并前的集合数
        for(i=1; i<=sum; i++)
        {
            msize[i]=1;
            mem[i]=i;
            for(j=1; j<=4; j++)
                mway[i][j]=0;
        }

        for(i=1; i<=m; i++)
        {
            for(j=1; j<=n; j++)
            {
                scanf("%c",&c);
                t=(i-1)*n+j;//一维数组转化为二维数组。方便并查集。
                //注意不是i*j呀!!
                switch(c)//记录水管类型
                {
                case 'A':
                    mway[t][1]=mway[t][2]=1;
                    break;
                case 'B':
                    mway[t][1]=mway[t][3]=1;
                    break;
                case 'C':
                    mway[t][4]=mway[t][2]=1;
                    break;
                case 'D':
                    mway[t][4]=mway[t][3]=1;
                    break;
                case 'E':
                    mway[t][4]=mway[t][1]=1;
                    break;
                case 'F':
                    mway[t][3]=mway[t][2]=1;
                    break;
                case 'G':
                    mway[t][1]=mway[t][2]=mway[t][3]=1;
                    break;
                case 'H':
                    mway[t][1]=mway[t][2]=mway[t][4]=1;
                    break;
                case 'I':
                    mway[t][4]=mway[t][2]=mway[t][3]=1;
                    break;
                case 'J':
                    mway[t][1]=mway[t][4]=mway[t][3]=1;
                    break;
                case 'K':
                    mway[t][1]=mway[t][2]=mway[t][3]=mway[t][4]=1;
                }
            }
            getchar();
        }
        for(i=1; i<=m; i++)
            for(j=1; j<=n; j++)
            {
                t=(i-1)*n+j;
                if(mway[t][1]==1)//如果相通则加入集合
                    if(i>1&&mway[(i-2)*n+j][4]==1)//ио
                        toone(t,(i-2)*n+j);
                if(mway[t][2]==1)
                    if(j>1&&mway[(i-1)*n+j-1][3]==1)//вС
                        toone(t,(i-1)*n+j-1);
                if(mway[t][3]==1)
                    if(j<n&&mway[(i-1)*n+j+1][2]==1)//ср
                        toone(t,(i-1)*n+j+1);
                if(mway[t][4]==1)
                    if(i<m&&mway[i*n+j][1]==1)//об
                        toone(t,i*n+j);
            }
        printf("%d\n",sum);
    }
    return 0;
}

改良后的代码

#include <stdio.h>

int mem[10000];//用于建立并查集
int msize[10000];//记录集合中水管数
int mway[10000][5];//记录每块田里的水管类型
int sum;//记录集合数目。即所需水源数

int finder(int x)//找根节点加路径压缩
{
    int i,j,t;
    i=x;
    while(mem[i]!=i)
        i=mem[i];
    j=x;
    while(j!=i)
    {
        t=mem[j];
        mem[j]=i;
        j=t;
    }
    return i;
}
void toone(int a,int b)//加入集合
{
    int r1,r2;
    //printf("a:%d b:%d\n",a,b);
    r1=finder(a);
    r2=finder(b);
    //printf("r1:%d r2:%d\n",r1,r2);
    if(r1==r2)
        return ;
    if(msize[r1]>msize[r2])
    {
        mem[r2]=r1;
        msize[r1]+=msize[r2];
    }
    else
    {
        mem[r1]=r2;
        msize[r2]+=msize[r1];
    }
    sum--;
}
int main()
{
    int i,j,t,m,n;
    char c;

    while(scanf("%d%d",&m,&n)!=EOF)
    {
        getchar();
        if(m==-1&&n==-1)
            break;
        sum=m*n;//sum初始为合并前的集合数
        for(i=1; i<=sum; i++)
        {
            msize[i]=1;
            mem[i]=i;
            for(j=1; j<=4; j++)
                mway[i][j]=0;
        }

        for(i=1; i<=m; i++)
        {
            for(j=1; j<=n; j++)
            {
                scanf("%c",&c);
                t=(i-1)*n+j;//一维数组转化为二维数组。方便并查集。
                //注意不是i*j呀!!
                //这样按方向标记代码更简洁
                 if(c=='A'||c=='B'||c=='E'||c=='G'||c=='H'||c=='J'||c=='K')
                    mway[t][1]=1;//上
                 if(c=='A'||c=='C'||c=='F'||c=='G'||c=='H'||c=='I'||c=='K')
                    mway[t][2]=1;//左
                 if(c=='B'||c=='D'||c=='F'||c=='G'||c=='I'||c=='J'||c=='K')
                    mway[t][3]=1;//右
                 if(c=='C'||c=='D'||c=='E'||c=='H'||c=='I'||c=='J'||c=='K')
                    mway[t][4]=1;//下
            }
            getchar();
        }
        for(i=1; i<=m; i++)
            for(j=1; j<=n; j++)
            {
                t=(i-1)*n+j;
                if(mway[t][1]==1)//如果相通则加入集合
                    if(i>1&&mway[(i-2)*n+j][4]==1)//查找了上就不用查找下了
                        toone(t,(i-2)*n+j);
                if(mway[t][2]==1)
                    if(j>1&&mway[(i-1)*n+j-1][3]==1)//查找了左就不用查找右了
                        toone(t,(i-1)*n+j-1);
            }
        printf("%d\n",sum);
    }
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值