40求调~
查看原帖
40求调~
1490301
hzishi楼主2025/2/4 15:42

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;
}
2025/2/4 15:42
加载中...