zoukankan      html  css  js  c++  java
  • codeforces 984B Minesweeper

    题意:

    给出一个矩阵,如果一个格子是数字,那么与这个格子相邻的格子中有炸弹的数量必须等于这个格子中的数字;

    如果一个格子是空地,那么这个格子的所有相邻的格子中就不能有炸弹。

    判断这个矩阵是否合法。

    思路:

    暴力枚举即可,不过空地那里要注意,相邻的是数字也可以。

    代码:

     1 #include <stdio.h>
     2 #include <string.h>
     3 #include <algorithm>
     4 #include <ctype.h>
     5 using namespace std;
     6 const int N = 105;
     7 char mp[N][N];
     8 int main()
     9 {
    10     int n,m;
    11     scanf("%d%d",&n,&m);
    12     for (int i = 0;i < n;i++) scanf("%s",mp[i]);
    13     bool f = 0;
    14     for (int i = 0;i < n;i++)
    15     {
    16         for (int j = 0;j < m;j++)
    17         {
    18             if (mp[i][j] >= '1' && mp[i][j] <= '8')
    19             {
    20                 int t = mp[i][j] - '0';
    21                 int cnt = 0;
    22                 for (int x = -1;x <= 1;x++)
    23                 {
    24                     for (int y = -1;y <= 1;y++)
    25                     {
    26                         if (x == 0 && y == 0) continue;
    27                         int dx = i + x,dy = j + y;
    28                         if (dx < 0 || dx >= n) continue;
    29                         if (dy < 0 || dy >= m) continue;
    30                         if (mp[dx][dy] == '*') cnt++;
    31                     }
    32                 }
    33                 if (cnt != t) f = 1;
    34             }
    35             else if (mp[i][j] == '.')
    36             {
    37                 int cnt1 = 0,cnt2 = 0;
    38                 for (int x = -1;x <= 1;x++)
    39                 {
    40                     for (int y = -1;y <= 1;y++)
    41                     {
    42                         if (x == 0 && y == 0) continue;
    43                         int dx = i + x,dy = j + y;
    44                         if (dx < 0 || dx >= n) continue;
    45                         if (dy < 0 || dy >= m) continue;
    46                         cnt1++;
    47                         if (mp[dx][dy] != '*') cnt2++;
    48                     }
    49                 }
    50                 if (cnt1 != cnt2) f = 1;
    51             }
    52         }
    53     }
    54     if (f) puts("NO");
    55     else puts("YES");
    56     return 0;
    57 }
  • 相关阅读:
    MSSQL复制表
    分享职场心得《7》
    分享职场心得《2》
    分享职场心得《3》
    免费收录网站搜索引擎登录入口最新版
    读写分离,读写分离死锁解决方案
    分享职场心得《5》
    分享职场心得《6》
    分享职场心得《1》
    分享职场心得《4》
  • 原文地址:https://www.cnblogs.com/kickit/p/9046435.html
Copyright © 2011-2022 走看看