P2670. 扫雷游戏

考点

  • 模拟

题解

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <bits/stdc++.h>

using namespace std;

const int LEN = 301;

char table[LEN][LEN];

int walk[8][2] = {{0, 1}, {0, -1}, {-1, 0}, {1, 0}, {1, 1}, {-1, -1}, {-1, 1}, {1, -1}};

int N, M;

int main()
{
cin >> N >> M;
for (int i = 1; i <= N; ++i)
{
for (int j = 1; j <= M; ++j)
{
cin >> table[i][j];
}
}
for (int i = 1; i <= N; ++i, cout << endl)
{
for (int j = 1; j <= M; ++j)
{
if (table[i][j] == '*')
{
cout << '*';
continue;
}
int cnt = 0;
for (int k = 0; k < 8; ++k)
{
int x = i + walk[k][0], y = j + walk[k][1];
if (x < 1 || x > 300 || y < 1 || y > 300)
continue;
if (table[x][y] == '*')
++cnt;
}
cout << cnt;
}
}
return 0;
}

思路

逐个点进行八向搜索即可,搜索前注意判断是否越界