zoukankan      html  css  js  c++  java
  • 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图

  • 相关阅读:
    AI人脸识别SDK接入 — 参数优化篇(虹软)
    虹软人脸识别ArcFace2.0 Android SDK使用教程
    虹软免费人脸识别SDK注册指南
    python3+arcface2.0 离线人脸识别 demo
    Android利用RecyclerView实现列表倒计时效果
    将博客搬至CSDN
    Retrofit动态设置支持JSON和XML格式转换工厂
    Android 简单统计文本文件字符数、单词数、行数Demo
    [雨]个人项目设计分析
    Android:随机生成算数四则运算简单demo(随机生成2~4组数字,进行加减乘除运算)
  • 原文地址:https://www.cnblogs.com/get-an-AC-everyday/p/4485134.html
Copyright © 2011-2022 走看看