zoukankan      html  css  js  c++  java
  • ZOJ 2412 Farm Irrigation

    Farm Irrigation

    Time Limit: 2 Seconds      Memory Limit: 65536 KB

    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 #include <iostream>
     4 using namespace std;
     5 
     6 const int max_n = 2520;
     7 int door[max_n][4];
     8 int fa[max_n];
     9 int root[max_n];
    10 char map[51][51];
    11 
    12 int find(int x){
    13     return x == fa[x] ? x : fa[x] = find(fa[x]);
    14 }
    15 
    16 void Union(int x, int y){
    17     int xx = find(x);
    18     int yy = find(y);
    19     if (xx != yy){
    20         fa[xx] = yy;
    21     }
    22 }
    23     
    24 int main(){
    25     int n, m;
    26     int i, j, ans, t;
    27     int a, b;
    28     while(~scanf("%d%d", &n, &m) && n > 0 && m > 0){
    29         for (i = 0; i < n; i ++)
    30             scanf("%s", map[i]);
    31         t = 0;
    32         memset(door, 0, sizeof(door));
    33         memset(root, 0, sizeof(root));
    34         for (i = 0; i < n; i ++){ //将二维转化为一维数组 
    35             for (j = 0; j < m; j ++){
    36                 switch (map[i][j]){
    37                     case 'A': door[t][0] = door[t][1] = 1; break;
    38                     case 'B': door[t][1] = door[t][2] = 1; break;
    39                     case 'C': door[t][3] = door[t][0] = 1; break;
    40                     case 'D': door[t][2] = door[t][3] = 1; break;
    41                     case 'E': door[t][1] = door[t][3] = 1; break;
    42                     case 'F': door[t][0] = door[t][2] = 1; break;
    43                     case 'G': door[t][0] = door[t][1] = door[t][2] = 1; break;
    44                     case 'H': door[t][3] = door[t][0] = door[t][1] = 1; break;
    45                     case 'I': door[t][2] = door[t][3] = door[t][0] = 1; break;
    46                     case 'J': door[t][1] = door[t][2] = door[t][3] = 1; break;
    47                     case 'K': door[t][0] = door[t][1] = door[t][2] = door[t][3] = 1; break;
    48                 }
    49                 fa[t] = t;
    50                 t ++;
    51             }
    52         }
    53         for (i = 0; i < n -1; i ++){
    54             for(j = 0; j < m -1; j ++){
    55                 a = i*m +j;
    56                 b = a +1;
    57                 if(door[a][2] && door[b][0])//相当于二维数组的横向对应 
    58                     Union(a, b);// 将属于同一管道群的用并查集并起来 
    59                 b = a + m;
    60                 if(door[a][3] && door[b][1])//相当于二维数组的纵向对应 
    61                     Union(a, b);
    62             }
    63             a = i*m + j; //二维边界 
    64             b = a + m;
    65             if(door[a][3] && door[b][1])
    66                 Union(a, b);
    67         }
    68         for (j = 0; j < m -1; j ++){//二维边界 
    69             a = i*m +j;
    70             b = a + 1;
    71             if(door[a][2] && door[b][0])
    72                 Union(a, b);
    73         }
    74         ans = 0;
    75         for (i = 0; i < t; i ++)
    76             root[find(i)] = 1; //找到总共有几个管道群 
    77         for (i = 0; i < t; i ++)
    78             if(root[i])
    79                 ans ++; //统计结果 
    80         printf ("%d
    ", ans);
    81     }
    82     return 0;
    83 }
    View Code
  • 相关阅读:
    How Does Batch Normalization Help Optimization?【阅读笔记】
    连接局域网内其他机器虚拟机中的Linux服务器
    阿里云磁盘扩容
    apscheduler模块
    linux挖矿病毒
    jenkins-时间参数
    k8s-学习笔记11-容器探针
    k8s-学习笔记10-watch机制
    xfce4设置屏保/锁屏时间
    高清动图方案
  • 原文地址:https://www.cnblogs.com/shengshouzhaixing/p/3839737.html
Copyright © 2011-2022 走看看