codeforces984B 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

, where each cell is either empty, or contains a digit from 1 to 8

, or a bomb. The field is valid if for each cell:

  • if there is a digit k
in the cell, then exactly 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

neighboring cells).

Input

The first line contains two integers n

and m ( 1n,m100

) — the sizes of the field.

The next n

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



判断扫雷是否合法。

“.”的周围没有雷,相当于数字0。

使用一个数组记录每个格子周围的雷数,最后和原数组里的数字做比较。若有雷,就给他周围的格子+1。

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <stack>
#include <queue>

using namespace std;

char rec[105][105];
int n,m;

int dx[8]={-1,-1,-1,0,0,1,1,1};
int dy[8]={-1,0,1,1,-1,-1,0,1};

int temp[105][105];

int add(int x,int y){
    for(int i=0;i<8;i++){
        int nx=x+dx[i];
        int ny=y+dy[i];

        if(nx>=0 && ny>=0 && nx<n && ny<m){
            temp[nx][ny]++;
        }
    }
}

int main(){
    cin>>n>>m;
    for(int i=0;i<n;i++){
        cin>>rec[i];
    }

    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            if(rec[i][j]=='*'){
                add(i,j);
            }
        }
    }
    int flag=1;

    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            if(rec[i][j]=='.' && temp[i][j]!=0){
                flag=0;
                break;
            }
            else if(rec[i][j]=='*') continue;
            else if(rec[i][j]=='.') continue;
            else if(rec[i][j]!=temp[i][j]+'0'){
                flag=0;
                break;
            }
        }
        if(flag==0) break;
    }

    if(flag==0){
        cout<<"NO"<<endl;
    }
    else{
        cout<<"YES"<<endl;
    }

    return 0;
}

学习到了深搜遍历的方法

方向数组+控制边界

int dx[8]={-1,-1,-1,0,0,1,1,1};
int dy[8]={-1,0,1,1,-1,-1,0,1};

int temp[105][105];

int add(int x,int y){
    for(int i=0;i<8;i++){
        int nx=x+dx[i];
        int ny=y+dy[i];

        if(nx>=0 && ny>=0 && nx<n && ny<m){
            temp[nx][ny]++;
        }
    }
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值