40分帮忙看看
#include <iostream>
#include <vector>
using namespace std;
struct Direction {
int x, y;
};
int main() {
int rows, cols;
cin >> rows >> cols;
vector<vector<char>> map(rows + 1, vector<char>(cols + 1));
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= cols; j++) {
cin >> map[i][j];
}
}
const Direction directions[] = {
{-1, -1}, {-1, 0}, {-1, 1},
{0, -1}, {0, 1},
{1, -1}, {1, 0}, {1, 1}
};
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= cols; j++) {
if (map[i][j] != '*') {
int count = 0;
for (const auto& dir : directions) {
int newX = i + dir.x;
int newY = j + dir.y;
if (newX >= 1 && newX <= rows && newY >= 1 && newY <= cols && map[newX][newY] == '*') {
count++;
}
}
cout << count << " ";
} else {
cout << '*';
}
}
cout << endl;
}
return 0;
}