Codeforces Round #483 (Div. 2) B. Minesweeper

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×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.

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

给你一个n*m的矩阵,问是否合理。其中*表示炸弹,‘.’表示这一点附近八个方向都没有炸弹,其他数字表示这一点附近八个方向炸弹个数。

也就是给你的矩阵,‘*’是已知,判断别的点对不对。

我们处理字符矩阵,把是'*'的附近八个方向都加加放在另外一个数组中,最后对比原来的数组就可以。

#include
#include
#include
using namespace std;
int mp[105][105];
int ans[105][105];
int n,m;
int dir[][2]={-1,1, 0,1, 1,1, -1,0, 1,0, -1,-1, 0,-1, 1,-1};
int main()
{
while(~scanf("%d %d",&n,&m))
{   
   memset(mp,-1,sizeof(mp));
   memset(ans,0,sizeof(ans));
   char tp;
getchar(); 
for(int i=1;i<=n;i++)
  for(int j=1;j<=m;j++)
  {  
   scanf("%c",&tp);
   if(tp=='*')
   {mp[i][j]=-2;//-2表示有炸弹 
     for(int k=0;k<8;k++)
       {
        int tx,ty;
        tx=dir[k][0]+i; ty=dir[k][1]+j;
        if(i>=1&&i<=n&&j>=1&&j<=m&&mp[i][j]!=-1)
ans[tx][ty]++; 
 }
      }
   else if(tp=='.')
          mp[i][j]=0,ans[i][j]=0;//-1表示空 
           else  mp[i][j]=tp-'0';
   if(j==m)getchar();
  }
  int flag=1;
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
           {
            if(mp[i][j]!=-1&&mp[i][j]!=-2&&mp[i][j]!=ans[i][j])
  {//printf("%d %d %d %d",ans[i][j],mp[i][j],i,j);
  flag=0;
  break;
     }

if(flag)
printf("YES\n");
else printf("NO\n");
}    

return 0;

你可能感兴趣的:(acm入门)