zoukankan      html  css  js  c++  java
  • 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 }
    我自倾杯,君且随意
  • 相关阅读:
    解决CHM文件不能浏览的问题
    SAS宏功能(下)
    python一句话求素数
    SAS与DBMS系统(SqlServer)通信
    CSS3圆角
    水平居中和垂直居中
    滚动条的控制
    快来给你的桌面加一只可爱的蟑螂吧 那个人
    自己用的PHP缓存类
    jquery 全反选
  • 原文地址:https://www.cnblogs.com/nicetomeetu/p/5164676.html
Copyright © 2011-2022 走看看