题解
2024-06-16 16:57:27
发布于:天津
话不多说直接上代码
#include <iostream>
#include <climits>
int countMines(char grid[][105], int i, int j, int n, int m) {
int num = 0;
if (i > 0 && grid[i - 1][j] == '') num++;
if (j > 0 && grid[i][j - 1] == '') num++;
if (i < n && grid[i + 1][j] == '') num++;
if (j < m && grid[i][j + 1] == '') num++;
if (i > 0 && j > 0 && grid[i - 1][j - 1] == '') num++;
if (i > 0 && j < m && grid[i - 1][j + 1] == '') num++;
if (i < n && j > 0 && grid[i + 1][j - 1] == '') num++;
if (i < n && j < m && grid[i + 1][j + 1] == '') num++;
return num;
}
int main() {
int n, m;
stdcin >> n >> m;
char s[105][105];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
stdcin >> s[i][j];
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (s[i][j] == '') {
std::cout << '';
} else {
stdcout << countMines(s, i, j, n, m);
}
}
stdcout << '\n';
}
return 0;
}
这里空空如也
有帮助,赞一个