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
  • 相关阅读:
    SR-IOV(Single Root I/O Virtualization)
    DHCP&DHCPv6
    Linux 上的基础网络设备详解
    当Linux用尽内存-oom
    真爱了--网络工程师技能图谱
    程序员必备技能:如何画好架构图?
    Linux内存使用情况以及内存泄露情况
    Neutron 消息回调系统
    linux bridge
    OpenStack-Neutron-code
  • 原文地址:https://www.cnblogs.com/LK1994/p/3355825.html
Copyright © 2011-2022 走看看