• UVa 11110


      题目大意:给一个n*n的矩阵,其中放置n个数字,判断四连通的相同数字的个数是否等于n。

      Flood fill,本来没什么,用dfs判断一下就可以了,可是用scanf读取输入时TLE了,然后看到别人说要用gets()读一整行,因为每行不一定是n对数,好吧,怎么感觉有点坑呢...把字符串解析成数字有没有什么好办法?...

     1 #include <cstdio>
     2 #include <cctype>
     3 #include <cstring>
     4 #define MAXN 110
     5 
     6 int mat[MAXN][MAXN];
     7 bool vis[MAXN][MAXN];
     8 int n, cnt;
     9 const int dir[4][2] = {{-1, 0}, {0, -1}, {0, 1}, {1, 0}};
    10 
    11 void dfs(int x, int y)
    12 {
    13     vis[x][y] = 1;
    14     cnt++;
    15     for (int i = 0; i < 4; i++)
    16     {
    17         int dx = x + dir[i][0];
    18         int dy = y + dir[i][1];
    19         if (dx >= 0 && dx < n && dy >= 0 && dy < n && mat[dx][dy] == mat[x][y] && !vis[dx][dy])
    20             dfs(dx, dy);
    21     }
    22 }
    23 
    24 int main()
    25 {
    26 #ifdef LOCAL
    27     freopen("in", "r", stdin);
    28 #endif
    29     int x, y;
    30     char str[10000];
    31     while (scanf("%d", &n) && n)
    32     {
    33         getchar();
    34         memset(mat, 0, sizeof(mat));
    35         for (int i = 1; i < n; i++)
    36         {
    37             gets(str);
    38             int len = strlen(str);
    39             for (int j = 0; j < len; )
    40             {
    41                 while (j < len && !isdigit(str[j]))  j++;
    42                 if (j == len)  break;
    43                 x = str[j] - '0';
    44                 j++;
    45                 while (j < len && isdigit(str[j]))
    46                 {
    47                     x = x * 10 + str[j] - '0';
    48                     j++;
    49                 }
    50                 while (j < len && !isdigit(str[j]))  j++;
    51                 y = str[j] - '0';
    52                 j++;
    53                 while (j < len && isdigit(str[j]))
    54                 {
    55                     y = y * 10 + str[j] - '0';
    56                     j++;
    57                 }
    58                 mat[x-1][y-1] = i;
    59             }
    60         }
    61         memset(vis, 0, sizeof(vis));
    62         bool ok = true;
    63         for (int i = 0; i < n; i++)
    64             for (int j = 0; j < n; j++)
    65                 if (!vis[i][j])
    66                 {
    67                     cnt = 0;
    68                     dfs(i, j);
    69                     if (cnt != n)
    70                     {
    71                         ok = false;
    72                         goto s;
    73                     }
    74                 }
    75 s:        if (ok)  printf("good
    ");
    76         else  printf("wrong
    ");
    77     }
    78     return 0;
    79 }
    View Code
  • 相关阅读:
    JSP和Servlet的相同点和不同点、有何联系。
    Java泛型
    HttpURLConnection与HttpClient比较和使用示例
    Mybatis中的#和$的区别
    MySQL_第三方数据库引擎_tokudb
    mysql的并发处理机制_上篇
    SQL SERVER 自动生成 MySQL 表结构及索引 的建表SQL
    SQL SERVER大话存储结构(6)_数据库数据文件
    SQL SERVER大话存储结构(4)_复合索引与包含索引
    基于binlog来分析mysql的行记录修改情况(python脚本分析)
  • 原文地址:https://www.cnblogs.com/xiaobaibuhei/p/3317690.html
Copyright © 2020-2023  润新知