前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Codeforces Round #483 (Div. 2) B. Minesweeper

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

作者头像
用户2965768
发布2018-08-30 16:24:30
5060
发布2018-08-30 16:24:30
举报
文章被收录于专栏:wymwym

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 (1≤n,m≤1001≤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

代码语言:javascript
复制
3 3
111
1*1
111

output

Copy

代码语言:javascript
复制
YES

input

Copy

代码语言:javascript
复制
2 4
*.*.
1211

output

Copy

代码语言:javascript
复制
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 <iostream> #include <algorithm> #include <cstring> 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; } 

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018年05月16日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档