Codeforces Round #483 (Div. 2) B题

◇◆丶佛笑我妖孽 提交于 2020-03-20 09:42:01
B. Minesweeper
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

One day Alex decided to remember childhood when computers were not too powerful and lots of people played only default games. Alex enjoyed playing Minesweeper that time. He imagined that he saved world from bombs planted by terrorists, but he rarely won.

Alex has grown up since then, so he easily wins the most difficult levels. This quickly bored him, and he thought: what if the computer gave him invalid fields in the childhood and Alex could not win because of it?

He needs your help to check it.

A Minesweeper field is a rectangle n×m

, where each cell is either empty, or contains a digit from 1 to 8

, or a bomb. The field is valid if for each cell:

  • if there is a digit k

in the cell, then exactly k

  • neighboring cells have bombs.
  • if the cell is empty, then all neighboring cells have no bombs.

Two cells are neighbors if they have a common side or a corner (i. e. a cell has at most 8

neighboring cells).

Input

The first line contains two integers n

and m (1n,m100

) — the sizes of the field.

The next n

lines contain the description of the field. Each line contains m characters, each of them is "." (if this cell is empty), "*" (if there is bomb in this cell), or a digit from 1 to 8

, inclusive.

Output

Print "YES", if the field is valid and "NO" otherwise.

You can choose the case (lower or upper) for each letter arbitrarily.

Examples
Input
Copy
3 31111*1111
Output
Copy
YES
Input
Copy
2 4*.*.1211
Output
Copy
NO
Note

In the second example the answer is "NO" because, if the positions of the bombs are preserved, the first line of the field should be *2*1.

You can read more about Minesweeper in Wikipedia's article.

思路:简单搜索

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <vector>
//codeforces
using namespace std;
char maps[110][110];
int dir[8][2]={{1,0},{1,1},{0,1},{-1,1},{-1,0},{-1,-1},{0,-1},{1,-1}};
int n,m;
int dfs(int x,int y)
{
    int tx,ty;
    if(maps[x][y]=='.'){
        for(int i=0;i<8;i++){
            tx=x+dir[i][0];
            ty=y+dir[i][1];
            if(tx<0||tx>=n||ty<0||ty>=m) continue;
            if(maps[tx][ty]=='*'){
                return 1;
            }
        }
    }
    int tmpe,countt=0;
    if(maps[x][y]<='8'&&maps[x][y]>='1'){
        tmpe=maps[x][y]-'0';
        for(int i=0;i<8;i++){
            tx=x+dir[i][0];
            ty=y+dir[i][1];
            if(tx<0||tx>=n||ty<0||ty>=m) continue;
            if(maps[tx][ty]=='*'){
                countt++;
            }
        }
        if(countt!=tmpe) return 1;
    }
    return 0;
}
int main()
{
    int flag;
    while(scanf("%d %d",&n,&m)!=EOF){
            flag=0;
    for(int i=0;i<n;i++){
        scanf("%s",maps[i]);
    }
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            if(maps[i][j]=='.'){
                if(dfs(i,j)){
                        flag=1;
                    break ;
                }
            }
            if(maps[i][j]<='8'&&maps[i][j]>='1'){
                if(dfs(i,j)){
                    flag=1;break;
                }
            }
        }
    }
    if(flag) printf("NO\n");
    else printf("YES\n");
    }
    return 0;
}

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!