CF984B Minesweeper

原题链接:http://codeforces.com/contest/984/problem/B

Minesweeper

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 n × m , where each cell is either empty, or contains a digit from 1 1 to 8 8 , or a bomb. The field is valid if for each cell:

if there is a digit k k in the cell, then exactly k 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 8 neighboring cells).

Input

The first line contains two integers n n and m(1n,m100) m ( 1 ≤ n , m ≤ 100 ) — the sizes of the field.

The next n n lines contain the description of the field. Each line contains m m characters, each of them is “.” (if this cell is empty), “*” (if there is bomb in this cell), or a digit from 1 1 to 8 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

3 3
111
1*1
111

output

YES

input

2 4
..
1211

output

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
using namespace std;
const int M=105;
int c1[]={0,1,1, 1, 0,-1,-1,-1};
int c2[]={1,1,0,-1,-1,-1, 0, 1};
int mmp[M][M],n,m;
char ch[M];
void in()
{
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;++i)
    {
        scanf("%s",ch+1);
        for(int j=1;j<=m;++j)
        if(isdigit(ch[j]))mmp[i][j]=ch[j]-'0';
        else if(ch[j]=='*')mmp[i][j]=100;
    }
}
void ac()
{
    for(int i=1;i<=n;++i)
    for(int j=1,cot;j<=m;++j)
    {
        cot=0;
        if(mmp[i][j]<9)
        {
            for(int k=0;k<8;++k)if(mmp[i+c1[k]][j+c2[k]]>9)++cot;
            if(cot!=mmp[i][j])puts("NO"),exit(0);
        }
    }
    puts("YES");
}
int main(){in();ac();}

你可能感兴趣的:(模拟,杂============)