zoukankan      html  css  js  c++  java
  • Farm Irrigation(并查集)

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


    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
     1 #include<stdio.h>
     2 #include<string.h>
     3 const int N = 100;
     4 int n,m;
     5 char map[N][N];
     6 int set[N*N];
     7 int pipe[11][4] = {{1,0,0,1},{1,1,0,0},{0,0,1,1},{0,1,1,0},
     8                    {1,0,1,0},{0,1,0,1},{1,1,0,1},{1,0,1,1},
     9                    {0,1,1,1},{1,1,1,0},{1,1,1,1}
    10                    };//十一种水管,每四个数字描述一种管子,表示它的上右下左
    11                     //是否有管口;
    12 
    13 
    14 void init()
    15 {
    16     for(int i = 0; i < n*m; i++)
    17             set[i] = i;
    18 }
    19 
    20 int find(int x)
    21 {
    22     if(set[x] != x)
    23         set[x] = find(set[x]);
    24     return set[x];
    25 }
    26 
    27 int judge(char a, char b, int pos)
    28 {
    29     int x = a-'A';
    30     int y = b-'A';
    31 
    32     if(pos)
    33     {
    34         if(pipe[x][2] == 1 && pipe[y][0] == 1)
    35             return 1;
    36         else return 0;
    37     }
    38     else
    39     {
    40         if(pipe[x][1] == 1 && pipe[y][3] == 1)
    41             return 1;
    42         else return 0;
    43     }
    44 }
    45 
    46 void merge(int a,int b)
    47 {
    48     a = find(a);
    49     b = find(b);
    50     set[a] = b;
    51 }
    52 
    53 int main()
    54 {
    55     int i,j;
    56     while(~scanf("%d %d",&n,&m))
    57     {
    58         if(n == -1 && m == -1)
    59             break;
    60         getchar();
    61         init();
    62         for(i = 0; i < n; i++)
    63             gets(map[i]);
    64 
    65         for(i = 0; i < n; i++)
    66         {
    67             for(j = 0; j < m; j++)
    68             {
    69                 if(i != n-1)
    70                 {
    71                     if(judge(map[i][j],map[i+1][j],1))//1表示两个字母所代表的管子位置关系是上下;
    72                     {
    73                         merge(i*m+j,(i+1)*m+j);
    74                     }
    75                 }
    76                 if(j != m-1)
    77                 {
    78                     if(judge(map[i][j],map[i][j+1],0))//0表示两个字母所代表的管子位置关系是左右;
    79                     {
    80                         merge(i*m+j,i*m+j+1);
    81                     }
    82                 }
    83             }
    84         }
    85         int count = 0;
    86         for(i = 0; i < n*m; i++)
    87         {
    88             if(set[i] == i)
    89                 count++;
    90         }
    91         printf("%d
    ",count);
    92 
    93     }
    94     return 0;
    95 }
    View Code
  • 相关阅读:
    MPI linux Ubuntu cluster 集群
    python cython c 性能对比
    TCAM CAM 说明 原理 结构 Verilog 硬件实现
    verilog 计算机网络 仿真 激励 pcap
    libtrace 安装 使用 修改
    Dream Spark ------spark on yarn ,yarn的配置
    Dream_Spark-----Spark 定制版:005~贯通Spark Streaming流计算框架的运行源码
    Dream_Spark-----Spark 定制版:003~Spark Streaming(三)
    Dream_Spark-----Spark 定制版:004~Spark Streaming事务处理彻底掌握
    Dream_Spark定制第二课
  • 原文地址:https://www.cnblogs.com/LK1994/p/3355825.html
Copyright © 2011-2022 走看看