• HDU 1198


    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
     
    大致题意:
      如上图11种土地块,块中的绿色线条为土地块中修好的水渠,现在一片土地由上述的各种土地块组成,需要浇水,问需要打多少口井。
    解题思路:
      并查集,查找不同集合数目,就是根节点数目。
     1 #include <cstdio>
     2 using namespace std;
     3 int pip[11][4]={{1,1,0,0},{0,1,1,0},{1,0,0,1},{0,0,1,1},
     4                 {0,1,0,1},{1,0,1,0},{1,1,1,0},{1,1,0,1},
     5                 {1,0,1,1},{0,1,1,1},{1,1,1,1}};
     6 int f[3000];    char map[50][50];
     7 int sf(int x) { return x==f[x]?x:f[x]=sf(f[x]);};
     8 int main(){
     9     int m,n;
    10     while(scanf("%d%d",&m,&n),m+n>0){
    11         for(int i=0;i<m*n;i++)    f[i]=i;
    12         for(int i=0;i<m;i++)    scanf("%s",&map[i]);
    13         for(int i=0;i<m;i++) //开始处理 
    14             for(int j=0;j<n;j++){
    15                 int x=map[i][j]-'A';
    16                 if(i+1<m){
    17                    int d=map[i+1][j]-'A';
    18                    if(pip[x][3]&&pip[d][1]){//和右方连 
    19                         int fx=sf(i*n+j);
    20                         int fa=sf((i+1)*n+j);
    21                         f[fx]=fa;
    22                     } 
    23                 }
    24                 if(j+1<n){
    25                     int r=map[i][j+1]-'A';
    26                     if(pip[x][2]&&pip[r][0]){//和下方连 
    27                         int fx=sf(i*n+j);
    28                         int fa=sf(i*n+j+1);
    29                         f[fx]=fa;
    30                     } 
    31                 }
    32             } 
    33         int ans=0;
    34         for(int i=0;i<m*n;i++) if(f[i]==i) ans++;
    35         printf("%d
    ",ans);
    36     } return 0;
    37 }
    我自倾杯,君且随意
  • 相关阅读:
    解决网站出现Error Establishing Database Connection问题
    Linux发行版时间线分支图最新版
    rem.js,移动多终端适配
    几种常用JavaScript设计模式es6
    文件上传,8种场景
    react动态添加样式:style和className
    记录我的 python 学习历程-Day13 匿名函数、内置函数 II、闭包
    记录我的 python 学习历程-Day12 生成器/推导式/内置函数Ⅰ
    记录我的 python 学习历程-Day11 两个被忽视的坑、补充知识点、函数名的应用、新版格式化输出、迭代器
    记录我的 python 学习历程-Day10 函数进阶
  • 原文地址:https://www.cnblogs.com/nicetomeetu/p/5164676.html
Copyright © 2020-2023  润新知