P2670 [NOIP2015 普及组] 扫雷游戏

// Problem: P2670 [NOIP2015 普及组] 扫雷游戏
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P2670
// Memory Limit: 125 MB
// Time Limit: 1000 ms
// User: Pannnn

#include <bits/stdc++.h>

using namespace std;

int main() {
    int n, m;
    cin >> n >> m;
    char info[110][110] = { 0 };
    
    // x/y从1开始读入,避免边界判断
    /*
    00000
    0*??0
    0???0
    0?*?0
    00000
    */
    for (int i = 1; i <= n; ++i) {
        for (int j = 1; j <= m; ++j) {
            cin >> info[i][j];
        }
    }
    
    // 使用2个数组纪录周围八个点相对中心的x/y的偏移,遍历数组i+dx[k],j+dy[k]即是周围点的坐标
    int dx[8] = { -1, -1, 0, 1, 1, 1, 0, -1 };
    int dy[8] = { 0, -1, -1, -1, 0, 1, 1, 1 };
    for (int i = 1; i <= n; ++i) {
        for (int j = 1; j <= m; ++j) {
            if (info[i][j] == '*') {
                continue;
            }
            int cnt = 0;
            for (int k = 0; k < 8; ++k) {
                if (info[i + dx[k]][j + dy[k]] == '*') {
                    cnt++;
                }
            }
            info[i][j] = '0' + cnt;
        }
    }
    
    for (int i = 1; i <= n; ++i) {
        for (int j = 1; j <= m; ++j) {
            cout << info[i][j];
        }
        cout << endl;
    }
    return 0;
}
上一篇:k8s yaml文件详解


下一篇:P2615 [NOIP2015 提高组] 神奇的幻方