CodeforcesRound#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 rectanglen×mn×m, where each cell is either empty, or contains a digit from11to88, or a bomb. The field is valid if for each cell:

  • if there is a digitkkin the cell, then exactlykkneighboring 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 most88neighboring cells).

Input

The first line contains two integersnnandmm(1≤n,m≤1001≤n,m≤100) — the sizes of the field.

The nextnnlines contain the description of the field. Each line containsmmcharacters, each of them is "." (if this cell is empty), "*" (if there is bomb in this cell), or a digit from11to88, 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 inWikipedia'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 "); else printf("NO "); } return 0; }

优质内容筛选与推荐>>
1、vue 2.0 全局引入eCharts以及按需引用eCharts
2、【计算机网络】计算机网络模型
3、用函数计算面积
4、网络攻防实验二
5、谈谈C#中的三个关键词new , virtual , override


长按二维码向我转账

受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。

    阅读
    好看
    已推荐到看一看
    你的朋友可以在“发现”-“看一看”看到你认为好看的文章。
    已取消,“好看”想法已同步删除
    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号