# #483(div.2) 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×mn×m, where each cell is either empty, or contains a digit from 11 to 88, or a bomb. The field is valid if for each cell:

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

Input

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

The next nn lines contain the description of the field. Each line contains mm characters, each of them is "." (if this cell is empty), "*" (if there is bomb in this cell), or a digit from 11 to 88, 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 3
111
1*1
111

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.

#include<iostream>
#include<cstring>
using namespace std;
int n,m;
char maze[101][101];
int d[101][101];
int dx[8]={-1,-1,-1,0,0,1,1,1},dy[8]={-1,0,1,1,-1,-1,0,1};
{
int i;
for(i=0;i<8;i++)
{
int nx=x+dx[i],ny=y+dy[i];
if(nx>=0&&nx<n&&ny>=0&&ny<m&&maze[nx][ny]!='*')d[nx][ny]=d[nx][ny]+1;
}
}
int main()
{
while(cin>>n>>m)
{
memset(d,0,sizeof(d));
int i,j;
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
cin>>maze[i][j];
}
}
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
}
}
int k=0;
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
if(maze[i][j]=='.'&&d[i][j]!=0){k=1;break;}
else if(maze[i][j]=='.')continue;
else if(maze[i][j]=='*')continue;
else if((int)(maze[i][j])-48!=d[i][j]){k=1;break;}
}
}
if(k==0)cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
return 0;
}

• 评论

• 上一篇
• 下一篇