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 }

 

转载于:https://www.cnblogs.com/kickit/p/9046435.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值