• hdu 1198 Farm Irrigation


    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
    题目大意:看起来很复杂,但其实是一道比较简单的问题,就是有点烦,首先给了你A~K种水管的铺法,然后给你一个图,问要是所有的方块都通水至少需要多少水源。
    分析:实际上就是求连通分支数,主要就是构图,我用了一个很2的方法建了个4维的数组来记录图。
     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #define maxlen 60
     5 using namespace std;
     6 char maps[maxlen][maxlen];
     7 bool visited[maxlen][maxlen];
     8 bool g[maxlen][maxlen][maxlen][maxlen];
     9 int n,m;
    10 int di[][4]= {{1,0,0,1},{1,1,0,0},{0,0,1,1},{0,1,1,0},{1,0,1,0},{0,1,0,1},{1,1,0,1},{1,0,1,1},{0,1,1,1},{1,1,1,0},{1,1,1,1}};
    11 int xx[]= {-1,0,1,0};
    12 int yy[]= {0,1,0,-1};
    13 bool judge(int x,int y)
    14 {
    15     return x>=1&&x<=n&&y>=1&&y<=m;
    16 }
    17 int dfs(int i,int j)
    18 {
    19     if(!judge(i,j)||visited[i][j])
    20         return 0;
    21     visited[i][j]=1;
    22     for(int k=0; k<4; ++k)
    23     {
    24         int nextx=i+xx[k];
    25         int nexty=j+yy[k];
    26         if(judge(nextx,nexty)&&!visited[nextx][nexty]&&g[i][j][nextx][nexty])
    27             dfs(nextx,nexty);
    28     }
    29     return 1;
    30 }
    31 int main ()
    32 {
    33     while(scanf("%d%d",&n,&m)!=EOF)
    34     {
    35         if(n<=0||m<=0)
    36             break;
    37         memset(g,0,sizeof(g));
    38         memset(maps,0,sizeof(maps));
    39         for(int i=1; i<=n; ++i)
    40         {
    41             for(int j=1; j<=m; ++j)
    42             {
    43                 g[i][j][i][j]=1;
    44             }
    45         }
    46         for(int i=1; i<=n; ++i)
    47             for(int j=1; j<=m; ++j)
    48                 cin>>maps[i][j];
    49         for(int i=1; i<=n; ++i)
    50         {
    51             for(int j=1; j<=m; ++j)
    52             {
    53                 for(int k=0; k<4; ++k)
    54                 {
    55                     if(di[maps[i][j]-'A'][k]&&judge(i+xx[k],j+yy[k])&&di[maps[i+xx[k]][j+yy[k]]-'A'][(k+2)%4])
    56                     {
    57                         g[i][j][i+xx[k]][j+yy[k]]=g[i+xx[k]][j+yy[k]][i][j]=1;
    58                     }
    59                 }
    60             }
    61         }
    62         memset(visited,0,sizeof(visited));
    63         int ans=0;
    64         for(int i=1; i<=n; ++i)
    65         {
    66             for(int j=1; j<=m; ++j)
    67             {
    68                 if(!visited[i][j])
    69                 {
    70                     ans+=dfs(i,j);
    71                 }
    72             }
    73         }
    74         printf("%d
    ",ans);
    75     }
    76 }
    View Code
  • 相关阅读:
    Bootstrap按钮
    Bootstrap表单
    Bootstrap表格
    Bootstrap列表
    jq 的onchange事件
    php 跳出循环的几种方式
    php变量和字符串连接符——点
    php 从2维数组组合为四维数组分析
    mysql 删除表中记录
    mysql 聚合函数
  • 原文地址:https://www.cnblogs.com/shuzy/p/3329371.html
Copyright © 2020-2023  润新知