• UVa 469


      题目大意:这个也是关于Flood fill的,给一个0和1构成的矩阵,指定一个位置(该位置值为1),计算包含该位置的连通分量中元素的个数。

     1 #include <cstdio>
     2 #include <cctype>
     3 #include <cstring>
     4 #define MAXN 110
     5 
     6 int G[MAXN][MAXN];
     7 bool vis[MAXN][MAXN];
     8 int n, m, ans;
     9 
    10 void dfs(int x, int y)
    11 {
    12     if (x < 0 || x >= n || y < 0 || y >= m || vis[x][y] || !G[x][y])  return;
    13     vis[x][y] = 1;
    14     ans++;
    15     dfs(x-1, y-1);  dfs(x-1, y);  dfs(x-1, y+1);
    16     dfs(x, y-1);                  dfs(x, y+1);
    17     dfs(x+1, y-1);  dfs(x+1, y);  dfs(x+1, y+1);
    18 }
    19 
    20 int main()
    21 {
    22 #ifdef LOCAL
    23     freopen("in", "r", stdin);
    24 #endif
    25     int T;
    26     scanf("%d", &T);
    27     getchar();
    28     char str[150];
    29     gets(str);
    30     while (T--)
    31     {
    32         n = 0;
    33         while (gets(str))
    34         {
    35             if (isdigit(str[0]))  break;
    36             m = strlen(str);
    37             for (int i = 0; i < m; i++)
    38             {
    39                 if (str[i] == 'L')  G[n][i] =  0;
    40                 else if (str[i] == 'W')  G[n][i] = 1;
    41             }
    42             n++;
    43         }
    44         int x, y;
    45         do
    46         {
    47             sscanf(str, "%d%d", &x, &y);
    48             x--;
    49             y--;
    50             ans = 0;
    51             memset(vis, 0, sizeof(vis));
    52             dfs(x, y);
    53             printf("%d
    ", ans);
    54         } while (gets(str) && str[0]);
    55         if (T)  printf("
    ");
    56     }
    57     return 0;
    58 }
    View Code
  • 相关阅读:
    Python_命名空间和作用域_25
    Python_函数_复习_习题_24
    Python_每日习题_0001_数字组合
    Python_试题_23
    Python_初识函数和返回值_22
    linux-shell-引用-命令替换-命令退出状态-逻辑操作符
    linux-shell-变量参数
    Python-复习-文件操作-21
    Python-注册登陆-20
    linux-vim
  • 原文地址:https://www.cnblogs.com/xiaobaibuhei/p/3315687.html
Copyright © 2020-2023  润新知