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

) — 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.

思路:简单搜索

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <vector>
//codeforces
using namespace std;
char maps[110][110];
int dir[8][2]={{1,0},{1,1},{0,1},{-1,1},{-1,0},{-1,-1},{0,-1},{1,-1}};
int n,m;
int dfs(int x,int y)
{
    int tx,ty;
    if(maps[x][y]=='.'){
        for(int i=0;i<8;i++){
            tx=x+dir[i][0];
            ty=y+dir[i][1];
            if(tx<0||tx>=n||ty<0||ty>=m) continue;
            if(maps[tx][ty]=='*'){
                return 1;
            }
        }
    }
    int tmpe,countt=0;
    if(maps[x][y]<='8'&&maps[x][y]>='1'){
        tmpe=maps[x][y]-'0';
        for(int i=0;i<8;i++){
            tx=x+dir[i][0];
            ty=y+dir[i][1];
            if(tx<0||tx>=n||ty<0||ty>=m) continue;
            if(maps[tx][ty]=='*'){
                countt++;
            }
        }
        if(countt!=tmpe) return 1;
    }
    return 0;
}
int main()
{
    int flag;
    while(scanf("%d %d",&n,&m)!=EOF){
            flag=0;
    for(int i=0;i<n;i++){
        scanf("%s",maps[i]);
    }
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            if(maps[i][j]=='.'){
                if(dfs(i,j)){
                        flag=1;
                    break ;
                }
            }
            if(maps[i][j]<='8'&&maps[i][j]>='1'){
                if(dfs(i,j)){
                    flag=1;break;
                }
            }
        }
    }
    if(flag) printf("NO\n");
    else printf("YES\n");
    }
    return 0;
}

Codeforces Round #483 (Div. 2) B题的更多相关文章

  1. Codeforces Round #483 (Div. 2)C题

    C. Finite or not? time limit per test 1 second memory limit per test 256 megabytes input standard in ...

  2. Codeforces Round #378 (Div. 2) D题(data structure)解题报告

    题目地址 先简单的总结一下这次CF,前两道题非常的水,可是第一题又是因为自己想的不够周到而被Hack了一次(或许也应该感谢这个hack我的人,使我没有最后在赛后测试中WA).做到C题时看到题目情况非常 ...

  3. Codeforces Round #612 (Div. 2) 前四题题解

    这场比赛的出题人挺有意思,全部magic成了青色. 还有题目中的图片特别有趣. 晚上没打,开virtual contest打的,就会前三道,我太菜了. 最后看着题解补了第四道. 比赛传送门 A. An ...

  4. Codeforces Round #713 (Div. 3)AB题

    Codeforces Round #713 (Div. 3) Editorial 记录一下自己写的前二题本人比较菜 A. Spy Detected! You are given an array a ...

  5. Codeforces Round #552 (Div. 3) A题

    题目网址:http://codeforces.com/contest/1154/problem/ 题目意思:就是给你四个数,这四个数是a+b,a+c,b+c,a+b+c,次序未知要反求出a,b,c,d ...

  6. Codeforces Round #412 Div. 2 补题 D. Dynamic Problem Scoring

    D. Dynamic Problem Scoring time limit per test 2 seconds memory limit per test 256 megabytes input s ...

  7. Codeforces Round #271 (Div. 2) E题 Pillars(线段树维护DP)

    题目地址:http://codeforces.com/contest/474/problem/E 第一次遇到这样的用线段树来维护DP的题目.ASC中也遇到过,当时也非常自然的想到了线段树维护DP,可是 ...

  8. Codeforces Round #425 (Div. 2))——A题&&B题&&D题

    A. Sasha and Sticks 题目链接:http://codeforces.com/contest/832/problem/A 题目意思:n个棍,双方每次取k个,取得多次数的人获胜,Sash ...

  9. Codeforces Round #483 (Div. 2) [Thanks, Botan Investments and Victor Shaburov!]

    题目链接:http://codeforces.com/contest/984 A. Game time limit per test:2 seconds memory limit per test:5 ...

随机推荐

  1. 【问题:发现与解决】angularJs指令在dijit控件中的使用

      由于公司主要用的JS框架是DOJO,最近又运用了angularJs.因此,不可能避免的遇到dojo控件和angular指令(directive)共用时遇到的一些问题,问题如下: <input ...

  2. C语言中关键词static的用法与作用域

    一.面向过程设计中的static 转载:http://blog.csdn.net/celerylxq/article/details/6160499 1.静态全局变量 在全局变量前,加上关键字stat ...

  3. CPU纯软件全虚拟化技术

    我们在前面的文章中提到了虚拟化技术的大致分类情况,即分为全虚拟化.半虚拟化和硬件辅助虚拟化3大类.而我们虚拟化技术最主要的虚拟主体就是我们的硬件CPU.内存和IO,那么我们的CPU在全虚拟化模式下如何 ...

  4. mac安装软件提示没有权限

    Mac 安装软件基本是各种爽,自动更新啥. 但是有一种提示没有权限的错误,很不爽,还要sudo管理员权限 有一个修复 /usr/local目录权限的命令 sudo chown -R 'whoami' ...

  5. Navicat for mysql 导出导入的问题

    问题现象   1:使用navicat 导出5.6.20版本数据库,然后导入到5.7.19mysql出现错误:  

  6. [翻译] HSDatePickerViewController

    HSDatePickerViewController HSDatePickerViewController is an iOS ViewController for date and time pic ...

  7. Linux命令--系统管理

    shutdown命令 Linux shutdown命令可以用来进行关机程序,并且在关机以前传送讯息给所有使用者正在执行的程序,shutdown 也可以用来重开机. 使用权限:系统管理者. 语法 shu ...

  8. 深入剖析php执行原理(4):函数的调用

    本章开始研究php中函数的调用和执行,先来看函数调用语句是如何被编译的. 我们前面的章节弄明白了函数体会被编译生成哪些zend_op指令,本章会研究函数调用语句会生成哪些zend_op指,等后面的章节 ...

  9. Git commit comment 汇总标准

    参考汇总互联网其它文章建议,结合PEP 257 Docstring Conventions的描述,总结的Git 注释风格,作为个人执行的标准.内容如下: 遵循标准: 1,所有注释尽量坚持使用英文,如果 ...

  10. Angular Reactive Form - 填充表单模型

    setValue 使用setValue,可以通过传递其属性与FormGroup后面的表单模型完全匹配的数据对象来一次分配每个表单控件值. 在分配任何表单控件值之前,setValue方法会彻底检查数据对 ...