• hdu.1198.Farm Irrigation(dfs +放大建图)


    Farm Irrigation

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

    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<stdio.h>
      2 #include<string.h>
      3 const int M = 55 ;
      4 char map[M][M] ;
      5 bool a[M * 3][M * 3] , vis[M * 3][M * 3];
      6 int move[][2] = {{1,0} , {-1,0} , {0,1} , {0,-1}} ;
      7 int n , m ;
      8 
      9 void build ()
     10 {
     11     memset (a , 0 , sizeof(a)) ;
     12     memset (vis , 0 , sizeof(vis)) ;
     13     for (int i = 0 ; i < n ; i ++) {
     14         for (int j = 0 ; j < m ; j ++) {
     15             int x = i * 3 + 1 , y = j * 3 + 1 ;
     16          //   printf ("(%d,%d)
    " , x , y ) ;
     17             a[x][y] = 1 ;
     18             if (map[i][j] == 'A') {
     19                 a[x][y - 1] = 1 ;
     20                 a[x - 1][y] = 1 ;
     21             }
     22             else if (map[i][j] == 'B') {
     23                 a[x - 1][y] = 1 ;
     24                 a[x][y + 1] = 1 ;
     25             }
     26             else if (map[i][j] == 'C') {
     27                 a[x + 1][y] = 1 ;
     28                 a[x][y - 1] = 1 ;
     29             }
     30             else if (map[i][j] == 'D') {
     31                 a[x + 1][y] = 1 ;
     32                 a[x][y + 1] = 1 ;
     33             }
     34             else if (map[i][j] == 'E') {
     35                 a[x + 1][y] = 1 ;
     36                 a[x - 1][y] = 1 ;
     37             }
     38             else if (map[i][j] == 'F') {
     39                 a[x][y + 1] = 1 ;
     40                 a[x][y - 1] = 1 ;
     41             }
     42             else if (map[i][j] == 'G') {
     43                 a[x][y - 1] = 1 ;
     44                 a[x][y + 1] = 1 ;
     45                 a[x - 1][y] = 1 ;
     46             }
     47             else if (map[i][j] == 'H') {
     48                 a[x][y - 1] = 1 ;
     49                 a[x - 1][y] = 1 ;
     50                 a[x + 1][y] = 1 ;
     51             }
     52             else if (map[i][j] == 'I') {
     53                 a[x][y - 1] = 1 ;
     54                 a[x][y + 1] = 1 ;
     55                 a[x + 1][y] = 1 ;
     56             }
     57             else if (map[i][j] == 'J') {
     58                 a[x - 1][y] = 1;
     59                 a[x + 1][y] = 1 ;
     60                 a[x][y + 1] = 1 ;
     61             }
     62             else if (map[i][j] == 'K') {
     63                 a[x - 1][y] = 1 ;
     64                 a[x + 1][y] = 1 ;
     65                 a[x][y - 1] = 1 ;
     66                 a[x][y + 1] = 1 ;
     67             }
     68         }
     69     }
     70     for (int i = 0 ; i < n * 3 ;  i ++) {
     71         for (int j = 0 ; j < m * 3 ; j ++) {
     72             if (a[i][j] == 0)
     73                 vis[i][j] = 1 ;
     74         }
     75     }
     76     //printf ("n=%d,m=%d
    " , n , m );
     77    /* for (int i = 0 ; i < n * 3 ; i ++) {
     78         for (int j = 0 ; j < m * 3 ; j ++) {
     79             printf ("%d " , a[i][j]) ;
     80         } puts ("") ;
     81     }*/
     82 }
     83 
     84 void dfs (int sx , int sy)
     85 {
     86     vis[sx][sy] = 1 ;
     87     int x , y ;
     88     for (int i = 0 ; i < 4 ; i ++) {
     89         x = sx + move[i][0] ; y = sy + move[i][1] ;
     90         if (a[x][y] && !vis[x][y]) {
     91             dfs (x , y) ;
     92         }
     93     }
     94 }
     95 
     96 int main ()
     97 {
     98     freopen ("a.txt" , "r" , stdin ) ;
     99     while (~ scanf ("%d%d" , &n , &m ) ) {
    100         if (n == -1 && m == -1) break ;
    101         for (int i = 0 ; i < n; i ++) scanf ("%s" , map[i] ) ; // puts (map[i]);
    102         build () ;
    103         int cnt = 0 ;
    104         for (int i = 0 ; i < 3 * n ; i ++) {
    105             for (int j = 0 ; j < 3 * m ; j ++) {
    106                 if ( !vis[i][j] && a[i][j]) {
    107                     dfs (i , j) ;
    108                     cnt ++;
    109                 }
    110             }
    111         }
    112         printf ("%d
    " , cnt ) ;
    113     }
    114     return 0 ;
    115 }
    View Code

    把地图放大3倍,建成一个0,1图

  • 相关阅读:
    [转载]三小时学会Kubernetes:容器编排详细指南
    《微服务设计》书摘
    Mac os安装golang开发环境
    openstack-KVM-Network
    openstack-KVM管理工具
    openstack-KVM-vCPU
    openstack-KVM-Memory
    openstack-KVM-存储配置
    openstack-云计算概述
    openstack-KVM安装与使用
  • 原文地址:https://www.cnblogs.com/get-an-AC-everyday/p/4485134.html
Copyright © 2020-2023  润新知