zoukankan      html  css  js  c++  java
  • 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
    ***************************************************************************************************************************
    联通块&&并查集
    ***************************************************************************************************************************
      1 #include<iostream>
      2 #include<string>
      3 #include<cstring>
      4 #include<cstdio>
      5 #include<queue>
      6 using namespace std;
      7 int farm[11][4]={
      8   {1,0,0,1},
      9   {1,1,0,0},
     10   {0,0,1,1},
     11   {0,1,1,0},
     12   {1,0,1,0},
     13   {0,1,0,1},
     14   {1,1,0,1},
     15   {1,0,1,1},
     16   {0,1,1,1},
     17   {1,1,1,0},
     18   {1,1,1,1}
     19 };
     20 int dir[4][2]={{0,-1},{-1,0},{0,1},{1,0}};
     21 int map[60][60];
     22 int fa[3600],n,m,i,j,k;
     23 int rank[3600];
     24 void init()
     25 {
     26    for(int it=0;it<=m*n;it++)
     27    {
     28       fa[it]=it;
     29       rank[it]=0;
     30    }
     31 }
     32 int find(int x)//查找
     33 {
     34   int r=x;
     35   while(r!=fa[r])r=fa[r];
     36   while(x!=r)
     37   {
     38     int st=fa[x];
     39     fa[x]=r;
     40     x=st;
     41   }
     42   return r;
     43 }
     44 void Unon(int a,int b)//合并
     45 {
     46   int x=find(a);
     47   int y=find(b);
     48   if(x!=y)
     49   {
     50       if(rank[x]>rank[y])
     51       {
     52           fa[y]=x;
     53       }
     54       else
     55       {
     56           fa[x]=y;
     57           if(rank[x]==rank[y])rank[y]++;
     58       }
     59   }
     60 }
     61 int main()
     62 {
     63     char ch;
     64   while(scanf("%d %d",&m,&n)!=EOF)
     65   {
     66       if(m<1||n<1)
     67         break;
     68       getchar();
     69       for(i=0;i<m;i++)
     70       {
     71           for(j=0;j<n;j++)
     72           {
     73               scanf("%c",&ch);
     74               map[i][j]=ch-'A';
     75           }
     76           getchar();
     77       }
     78       init();
     79       for(i=0;i<m;i++)
     80       {
     81           for(j=0;j<n;j++)
     82           {
     83               for(k=0;k<4;k++)
     84               {
     85                   int dx=i+dir[k][0];
     86                   int dy=j+dir[k][1];
     87                   if(dx<0||dx>=m||dy<0||dy>=n)
     88                     continue;
     89                   if(k==0)//
     90                   {
     91                      if(farm[map[i][j]][3]&&farm[map[dx][dy]][1])
     92                         Unon(dx*n+dy,i*n+j);
     93                   }
     94                   else
     95                    if(k==1)//
     96                    {
     97                       if(farm[map[i][j]][0]&&farm[map[dx][dy]][2])
     98                         Unon(dx*n+dy,i*n+j);
     99                    }
    100                    else
    101                     if(k==2)//
    102                      {
    103                       if(farm[map[i][j]][1]&&farm[map[dx][dy]][3])
    104                         Unon(dx*n+dy,i*n+j);
    105                      }
    106                      else
    107                       if(k==3)//
    108                       {
    109                         if(farm[map[i][j]][2]&&farm[map[dx][dy]][0])
    110                           Unon(dx*n+dy,i*n+j);
    111                       }
    112               }
    113           }
    114       }
    115       int ans=0;
    116       for(i=0;i<m*n;i++)
    117         if(fa[i]==i)//找连通块
    118             ans++;
    119 
    120       printf("%d
    ",ans);
    121   }
    122   return 0;
    123 }
    View Code
  • 相关阅读:
    Steal 偷天换日 题解(From luoguBlog)
    Hibernat之关系的处理多对多
    Hibernat之关系的处理一对一处理
    Hibernat之关系的处理一对多/多对一
    Hibernate 一对多注解 mappedby 作用
    hibernate之使用Annotation注解搭建项目
    暑假学习第八周
    暑假学习第七周
    java学习第六周
    暑假学习第五周
  • 原文地址:https://www.cnblogs.com/sdau--codeants/p/3406482.html
Copyright © 2011-2022 走看看