codeforce 984B. Minesweeper

B. Minesweeper
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard 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
×
m
n×m, where each cell is either empty, or contains a digit from
1
1 to
8
8, or a bomb. The field is valid if for each cell:

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

Input
The first line contains two integers
n
n and
m
m (
1

n
,
m

100
1≤n,m≤100) — the sizes of the field.

The next
n
n lines contain the description of the field. Each line contains
m
m characters, each of them is “.” (if this cell is empty), “*” (if there is bomb in this cell), or a digit from
1
1 to
8
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
inputCopy
3 3
111
1*1
111
outputCopy
YES
inputCopy
2 4
..
1211
outputCopy
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.

分析:

直接建图(二维数组存),注意输入的时候直接输入数组的每一行!根据样例输入得到!让后暴力找每一个数组中的元素,根据题目的意思都可以得到相应的解释 ,例如如果是空格,则周围都不能有任何炸弹!如果有的话直接输出NO 并直接退出程序!
同理,下面的也只需要判断不成立的情况!

ac代码:

#include<bits/stdc++.h>
using namespace std;
int a[8][2]={
{-1,-1},
{-1,0},
{-1,1},
{0,1},
{1,1},
{1,0},
{1,-1},
{0,-1}};
char mp[101][101];

int main()
{
    int n,m;
    cin>>n>>m;
        int i,j,k,falg=0,count=0;
        for(i=0;i<n;i++){
            scanf("%s",&mp[i]);
        }    //输入地图
        /*for(i=0;i<n;i++){
            for(j=0;j<m;j++){
                cout<<mp[i][j];
            }
            cout<<endl;
        }*/ 

        for(i=0;i<n;i++){
            for(j=0;j<m;j++){
                if(mp[i][j]=='.'){
                    for(k=0;k<8;k++){
                        if((i+a[k][0]>=0)&&(i+a[k][0]<n)&&(j+a[k][1]>=0)&&(j+a[k][1]<m)&&mp[i+a[k][0]][j+a[k][1]]=='*'){
                            cout<<"NO"<<endl;
                            return 0;
                        }
                    }
                }
                else if(mp[i][j]>'0'&&mp[i][j]<='8'){
                    for(k=0;k<8;k++){
                        if((i+a[k][0]>=0)&&(i+a[k][0]<n)&&(j+a[k][1]>=0)&&(j+a[k][1]<m)&&mp[i+a[k][0]][j+a[k][1]]=='*'){
                            count+=1;
                        }
                    }
                    if(count!=mp[i][j]-'0'){
                        cout<<"NO"<<endl;
                        return 0;
                    }
                    count=0;
                }
            }
        }

        cout<<"YES"<<endl;

    return 0;

}

猜你喜欢

转载自blog.csdn.net/wrf20162305/article/details/80342925
今日推荐