Codeforces-984 B-Minesweeper-暴力


リンクを解く:
https://www.lucien.ink/archives/210/
テーマリンク:
http://codeforces.com/contest/984/problem/B
タイトル:
One day Alex decided to remember childhold when computers were not torowerful and loople playd of people playd and default games.Alex enjoyed playing Minese weeper that time.Heimgisted that heved world fronters
Alex has grown up since then,so he easure wins the most difficult levels.This quickly bored him,and he thought:what if the compter gave hi invalid fields in the childhood and Alex could wind?
He needs your help to check it.
A Minese weeper field is a rectangle n×m n× m,where each cell is eigther empty,or contains a digit from 1 to 8,or a bomb.The field is valid for each cell:
  • if there is a digit k in the cell,then exactly k neigh boring cell have bombs.
  • if the cell is empty,then all neigh boring cell haveのbomb.
  • Two cels are neighbors if they have a common side or a coner(i.e.a cell has at most 8 neighboring cels)
    Input
    The first line contains two integers n and m(1≦n、m≦100)m(1≦n、m≦100)—the sizes of the field.
    The next nn line s contain the description of the field.Each line contains mm characters,each of the m is"."(if this cell is empty),「*」(if there is bomb in this cell),or a digit from 1 to 8,clinive.
    Output
    Print「YES」、if the field is valid and「NO」otherswise.
    You can chose the case for each letter arbitrally.
    件名:
    nを一つあげます×m n× mの8連通行列は、.がこの点の周囲を表しているから*がないとは限りません。もし数字であれば、この点の周りにx個の*があります。与えられたマトリックスが合法かどうか確認してください。
    考え方:
    暴力を振るってもいいです。
    実装:
    #include 
    using namespace std;
    const int maxn = 507;
    int n, m, nxt[8][2] = {{-1, -1}, {-1, 0}, {-1, 1}, {0, -1}, {0, 1}, {1, -1}, {1, 0}, {1, 1}};
    char mp[maxn][maxn];
    bool check(int x, int y) {
        if (mp[x][y] == '*') return true;
        int cnt = 0, aim = isdigit(mp[x][y]) ? mp[x][y] - '0' : 0;
        for (int k = 0; k < 8; k++) if (mp[x + nxt[k][0]][y + nxt[k][1]] == '*') cnt++;
        return cnt == aim;
    }
    int main() {
        cin >> n >> m;
        for (int i = 1; i <= n; i++) cin >> mp[i] + 1;
        for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) {
                if (check(i, j)) continue;
                return 0 * puts("NO");
            }
        return 0 * puts("YES");
    }