• HDU 1198 Farm Irrigation


    Farm Irrigation

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


    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



    比较有意思的模拟题,对于一个方块,用四个bool变量标记方块四个边上是否能连通管道,检查与相邻的方块是否匹配。

     1 #include <cstdio>
     2 #include <algorithm>
     3 using namespace std;
     4 const int MAX = 60;
     5 struct Square{
     6     int p[4];//西北东南
     7 };
     8 const Square s['K' - 'A' + 1] = { { 1, 1, 0, 0 }, { 0, 1, 1, 0 }, { 1, 0, 0, 1 }, { 0, 0, 1, 1 },
     9                                   { 0, 1, 0, 1 }, { 1, 0, 1, 0 }, { 1, 1, 1, 0 }, { 1, 1, 0, 1 },
    10                                   { 1, 0, 1, 1 }, { 0, 1, 1, 1 }, { 1, 1, 1, 1 } };
    11 char farm[MAX][MAX];
    12 bool t[MAX][MAX];
    13 int f[MAX*MAX];
    14 
    15 int sf(int x)
    16 {
    17     return x == f[x] ? x : f[x] = sf(f[x]);
    18 }
    19 int m, n;
    20 
    21 int dx[4] = {0,1, 0,-1};
    22 int dy[4] = {1,0,-1, 0};
    23 void merge(int x, int y)
    24 {
    25     for (int i = 0, j = 2; i < 4; i++, j++, j %= 4)
    26     {
    27         int nx = x + dx[i];
    28         int ny = y + dy[i];
    29         if (0 <= nx && nx < m && 0 <= ny && ny < n && s[farm[nx][ny] - 'A'].p[i]
    30             && s[farm[x][y]-'A'].p[j]) //可以吻合
    31         {
    32             f[sf(nx*n + ny)] = sf(x*n + y);
    33         }
    34     }
    35 }
    36 
    37 int main()
    38 {
    39     int ans;
    40     while (scanf("%d%d", &m, &n) == 2 && n>0 && m>0)
    41     {
    42         ans = 0;
    43         memset(t, 0, sizeof(t));
    44         for (int i = 0; i<m; ++i)
    45             scanf("%s", farm[i]);
    46 
    47         for (int i = 0, k = 0; i<m; ++i)
    48             for (int j = 0; j<n; ++j, ++k)
    49                 f[i*n + j] = k;
    50 
    51         for (int i = 0; i<m; ++i)
    52             for (int j = 0; j<n; ++j)
    53                 merge(i, j);
    54         for (int i = 0; i<n*m; i++)
    55         {
    56             if (i == f[i])
    57                 ans++;
    58         }
    59         /*
    60         for (int i = 0; i<n*m; i++)
    61         {
    62             printf("%d ", f[i]);
    63             if ((i + 1) % n == 0) printf("
    ");
    64         }
    65         */
    66         printf("%d
    ", ans);
    67     }
    68 }
  • 相关阅读:
    文件处理(记录经典代码及相关逻辑)
    python与pip在widow下cmd命令行操作问题收集:python、pip不是内部或外部命令,也不是可运行程序
    python—函数中存在的部分问题
    python —匿名函数(lambda)
    Python—递归函数
    python-函数参数
    python代码运行过程
    String类练习
    String类的常用方法
    死锁与进程通信
  • 原文地址:https://www.cnblogs.com/cumulonimbus/p/5169937.html
Copyright © 2020-2023  润新知