题解
2024-05-20 22:51:05
发布于:上海
8阅读
0回复
0点赞
#include <iostream>
#include <vector>
using namespace std;
int main() {
int n, m;
cin >> n >> m;
vector<vector<char>> grid(n, vector<char>(m));
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cin >> grid[i][j];
}
}
vector<vector<int>> count(n, vector<int>(m));
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (grid[i][j] == '*') continue;
for (int k = -1; k <= 1; k++) {
for (int l = -1; l <= 1; l++) {
int x = i + k, y = j + l;
if (x >= 0 && x < n && y >= 0 && y < m && grid[x][y] == '*') {
count[i][j]++;
}
}
}
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (grid[i][j] == '*') {
cout << '*';
} else {
cout << count[i][j];
}
}
cout << endl;
}
return 0;
}
这里空空如也
有帮助,赞一个