zoukankan      html  css  js  c++  java
  • Balloons dfs

     
     
     Both Saya and Kudo like balloons. One day, they heard that in the central park, there will be thousands of people fly balloons to pattern a big image.

    They were very interested about this event, and also curious about the image.

    Since there are too many balloons, it is very hard for them to compute anything they need. Can you help them?

    You can assume that the image is an N*N matrix, while each element can be either balloons or blank.

    Suppose element and element B are both balloons. They are connected if:

    i) They are adjacent;
           ii) There is a list of element C1C2, … , Cn, while A and C1 are connected, C1 and C2are connected …Cn and B are connected.

    And a connected block means that every pair of elements in the block is connected, while any element in the block is not connected with any element out of the block.

    To Saya, element A(xa,ya)and B(xb,yb) is adjacent if |xa-xb| + |ya-yb|  1 
           But to Kudo, element A(xa,ya) and element B (xb,yb) is adjacent if |xa-xb|≤1 and |ya-yb|1
           They want to know that there’s how many connected blocks with there own definition of adjacent?

    输入

    The input consists of several test cases.
    The first line of input in each test case contains one integer N (0<N100), which represents the size of the matrix.
    Each of the next N lines contains a string whose length is N, represents the elements of the matrix. The string only consists of 0 and 1, while 0 represents a block and 1represents balloons.
    The last case is followed by a line containing one zero.

    输出

     For each case, print the case number (1, 2 …) and the connected block’s numbers with Saya and Kudo’s definition. Your output format should imitate the sample output. Print a blank line after each test case.

    示例输入

    5
    11001
    00100
    11111
    11010
    10010
    
    0

    示例输出

    Case 1: 3 2
     1 #include<stdio.h>
     2  #include<string.h>
     3  int map[110][110],vis[110][110];
     4  void dfs1(int x,int y)
     5  {
     6      if(map[x][y] == 0 || vis[x][y] == 1)
     7          return;
     8      vis[x][y] = 1;
     9      dfs1(x,y-1);
    10      dfs1(x,y+1);
    11      dfs1(x+1,y);
    12      dfs1(x-1,y);
    13  
    14  }//四连块
    15  void dfs2(int x,int y)
    16  {
    17      if(map[x][y] == 0 || vis[x][y] == 1)
    18          return;
    19      vis[x][y] = 1;
    20      dfs2(x-1,y-1); dfs2(x-1,y);dfs2(x-1,y+1); dfs2(x,y-1);
    21      dfs2(x,y+1); dfs2(x+1,y-1); dfs2(x+1,y);  dfs2(x+1,y+1);
    22  }//八连块
    23  int main()
    24  {
    25  
    26      int n,cnt,sum;
    27      char s[110];
    28      int k =1,i,j;
    29      while(~scanf("%d",&n)&&n)
    30      {
    31          memset(vis,0,sizeof(vis));
    32          for(i = 0; i <= n+1; i++)
    33          {
    34              map[0][i] = 0;
    35              map[i][0] = 0;
    36              map[n+1][i] = 0;
    37              map[i][n+1] = 0;
    38          }外加一道墙
    39          for(i = 1; i <= n;i++)
    40          {
    41              scanf("%s",s);
    42              for(j = 0; j < n; j++)
    43              {
    44                  map[i][j+1] = s[j] - '0';
    45              }
    46          }
    47          cnt = 0,sum = 0;
    48          for(i = 1; i <= n; i++)
    49              for(j = 1; j <= n; j++)
    50                  if(!vis[i][j] && map[i][j])
    51                  {
    52                      cnt++;
    53                      dfs1(i,j);
    54                  }
    55          memset(vis,0,sizeof(vis));
    56          for(i = 1; i <= n; i++)
    57              for(j = 1; j <= n; j++)
    58                  if(!vis[i][j] && map[i][j])
    59                  {
    60                      sum++;
    61                      dfs2(i,j);
    62  
    63                  }
    64         printf("Case %d: %d %d\n\n",k++,cnt,sum);
    65      }
    66      return 0;
    67  
    68  }
    69  
  • 相关阅读:
    SAP 是不是很烂的一个ERP软件
    Linux 的目录树
    LINUX连接外网的安全问题(查看日志)
    硬盘概念:柱面、磁道、扇区、簇
    Linux下安装PHP pdo_mysql支持
    端口映射帮助文档
    怎么建设一个FTP服务器
    linux开启telnet服务
    CentOS yum的详细使用方法
    WinXP SSH连接不上虚拟机的解决方法
  • 原文地址:https://www.cnblogs.com/LK1994/p/3085566.html
Copyright © 2011-2022 走看看