zoukankan      html  css  js  c++  java
  • [ACM] hdu 1198 Farm Irrigation (看图枚举+并查集)

    Farm Irrigation

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


    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


    解题思路:

    并查集的题目。这题一开始想的是把每块田看成一个结构体,包含上下左右四个方向,看有没有通道,如果有,就为1,无就为0,相邻的节点如果相反的方向都为1,则可以合并为一个集合。但后来做了一阵子没做出来,后来换了一个思路。遍历每块田地,只要考虑它和左边和上边的田地是否能并合并就可以了。对于左边和上边哪两块能够合并,看图枚举就可以了。

    比如 当前地对左边的那个是否能合并,(左边田地)就找图中田地右边有管道的有B   D F G I J K        当前田地左边有管道的图中有 A C F G H I  K 这样两块田地就可以合并

    对当前地和上边的田地是否能合并,也是枚举出来。

    代码:

    #include <iostream>
    using namespace std;
    const int N=52;
    char maze[N][N];
    int parent[N*N];
    
    void init(int n)
    {
        for(int i=1;i<=n;i++)
            parent[i]=i;
    }
    int find(int x)
    {
        return parent[x]==x?x:find(parent[x]);
    }
    void unite(int x,int y)
    {
        x=find(x);
        y=find(y);
        if(x==y)
            return;
        parent[x]=y;
    }
    bool same(int x,int y)
    {
        return find(x)==find(y);
    }
    int main()
    {
        int n,m;
        while(cin>>n>>m&&n!=-1&&m!=-1)
        {
            init(n*m);//每块田地的编号 1~n*m 
            for(int i=1;i<=n;i++)
                for(int j=1;j<=m;j++)
            {
                cin>>maze[i][j];
            }
            for(int i=1;i<=n;i++)
                for(int j=1;j<=m;j++)
            {
                if(j-1>=1)//保证不越界
                {//当前田地是否能和左边的相邻的田地合并
                    if((maze[i][j-1]=='B'||maze[i][j-1]=='D'||maze[i][j-1]=='F'||maze[i][j-1]=='G'||maze[i][j-1]=='I'||maze[i][j-1]=='J'||maze[i][j-1]=='K')
                       &&(maze[i][j]=='C'||maze[i][j]=='F'||maze[i][j]=='G'||maze[i][j]=='H'||maze[i][j]=='I'||maze[i][j]=='A'||maze[i][j]=='K'))
                        unite((i-1)*m+j-1,(i-1)*m+j);
                }
                if(i-1>=1)
                {//当前田地是否能和上边的相邻的田地合并
                    if((maze[i-1][j]=='C'||maze[i-1][j]=='D'||maze[i-1][j]=='E'||maze[i-1][j]=='H'||maze[i-1][j]=='I'||maze[i-1][j]=='J'||maze[i-1][j]=='K')&&
                       (maze[i][j]=='A'||maze[i][j]=='B'||maze[i][j]=='E'||maze[i][j]=='G'||maze[i][j]=='H'||maze[i][j]=='J'||maze[i][j]=='K'))
                        unite((i-1-1)*m+j,(i-1)*m+j);
                }
            }
            int num=0;
            for(int i=1;i<=n*m;i++)
                if(parent[i]==i)
                num++;
            cout<<num<<endl;
        }
        return 0;
    }
    



  • 相关阅读:
    Python:起步
    实战Django:Rango Part8
    实战Django:Rango Part7
    实战Django:Rango Part6
    实战Django:Rango Part5
    实战Django:Rango Part4
    Fitnesse集成TestLink
    oracle存储过程代码覆盖率统计工具
    ant svn
    Jenkins中关于一些插件的使用方法
  • 原文地址:https://www.cnblogs.com/sr1993/p/3697906.html
Copyright © 2011-2022 走看看