题目描述

扫雷是一个经典的游戏,在一个$N*M$区域中 
输入一个$N*M$的雷区中的k个雷坐标,打印出所有雷区的分布 
一个位置如果是雷则表示为*,否则应该是0~8,表示他周围八连通域中的地雷总数。

输入

雷区尺寸:$3=<n,m<=20$
地雷数目:$k<=M*N$
和他们的坐标:$xi$,$yi$

输出

雷区的分布 
每组后空一行 

题解

#include<iostream>
#include <cstring>

using namespace std;
const int maxn = 21;
const int BOMB = 999;
int a[maxn][maxn];
int m, n, k, x, y;

void show() {
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            if (a[i][j] != BOMB) {
                cout << a[i][j];
            } else {
                cout << "*";
            }
        }
        cout << endl;
    }
}

void cal() {
    for (x = 0; x < n; x++)
        for (y = 0; y < m; y++) {
            if (a[x][y] == BOMB) {
                continue;
            }
            int &tot = a[x][y];
            for (int dx = -1; dx <= 1; ++dx)
                for (int dy = -1; dy <= 1; ++dy) {
                    if (dx == 0 && dy == 0) {
                        continue;
                    }
                    int nx = x + dx;
                    int ny = y + dy;
                    if (nx < 0 || nx >= n || ny < 0 || ny >= m) {
                        continue;
                    }
                    if (a[nx][ny] == BOMB) {
                        ++tot;
                    }
                }
        }
}

void input() {
    memset(a, 0, sizeof(a));
    while (k--) {
        cin >> x >> y;
        a[x][y] = BOMB;
    }
}

int main() {
    while (cin >> n >> m >> k) {
        input();
        cal();
        show();
        cout << endl;
    }
    return 0;
}