算法1
(暴力枚举) $O(n^2)$
初始矩阵各项均为0, 每读入一个地雷, 就把该地雷附近8个格子的值加1
C++ 代码
#include <iostream>
using namespace std;
const int N = 110;
int a[N][N];
bool mine[N][N];
void update(int x, int y) {
int dx[] = {-1, -1, -1, 0, 0, +1, +1, +1}, dy[] = {-1, 0, +1, -1, +1, -1, 0, +1};
for (int i = 0; i<8; i++) {
a[x+dx[i]][y+dy[i]]++;
}
}
int main() {
int n, m;
cin >> n >> m;
for (int i=1; i<=n; i++) {
for (int j=1; j<=m; j++) {
char c;
cin >> c;
if (c == '*') {
mine[i][j] = true;
update(i, j);
}
}
}
for (int i=1; i<=n; i++) {
for (int j=1; j<=m; j++) {
if (mine[i][j]) cout << '*';
else cout << a[i][j];
}
cout << endl;
}
return 0;
}