Codeforces Round #483 (Div. 2) B题
1 second
256 megabytes
standard input
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).
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.
Print "YES", if the field is valid and "NO" otherwise.
You can choose the case (lower or upper) for each letter arbitrarily.
3 3
111
1*1
111
YES
2 4
*.*.
1211
NO
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题的更多相关文章
- 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 ...
- Codeforces Round #378 (Div. 2) D题(data structure)解题报告
题目地址 先简单的总结一下这次CF,前两道题非常的水,可是第一题又是因为自己想的不够周到而被Hack了一次(或许也应该感谢这个hack我的人,使我没有最后在赛后测试中WA).做到C题时看到题目情况非常 ...
- Codeforces Round #612 (Div. 2) 前四题题解
这场比赛的出题人挺有意思,全部magic成了青色. 还有题目中的图片特别有趣. 晚上没打,开virtual contest打的,就会前三道,我太菜了. 最后看着题解补了第四道. 比赛传送门 A. An ...
- Codeforces Round #713 (Div. 3)AB题
Codeforces Round #713 (Div. 3) Editorial 记录一下自己写的前二题本人比较菜 A. Spy Detected! You are given an array a ...
- 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 ...
- 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 ...
- Codeforces Round #271 (Div. 2) E题 Pillars(线段树维护DP)
题目地址:http://codeforces.com/contest/474/problem/E 第一次遇到这样的用线段树来维护DP的题目.ASC中也遇到过,当时也非常自然的想到了线段树维护DP,可是 ...
- Codeforces Round #425 (Div. 2))——A题&&B题&&D题
A. Sasha and Sticks 题目链接:http://codeforces.com/contest/832/problem/A 题目意思:n个棍,双方每次取k个,取得多次数的人获胜,Sash ...
- 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 ...
随机推荐
- HDFS原理解析
一.HDFS简介 HDFS为了做到可靠性(reliability)创建了多分数据块(data blocks)的复制(replicas),并将它们放置在服务器群的计算节点中(computer nodes ...
- 【QT】在子窗体中控制父窗体
[背景说明]我的主窗体的名字叫做MainWindow,其子窗口是一个叫subDialog的类.我现在想做的是在子窗口的函数中调用父窗口. 在父窗口中打开子窗口 //弹出对话框确定变换的参数 subDi ...
- IDEA Properties中文unicode转码问题
在IDEA中创建了properties文件,发现默认中文不会自动进行unicode转码.如下 在project settings - File Encoding,在标红的选项上打上勾,确定即可 效果图 ...
- iOS设计模式 - 外观
iOS设计模式 - 外观 原理图 说明 1. 当客服端需要使用一个复杂的子系统(子系统之间关系错综复杂),但又不想和他们扯上关系时,我们需要单独的写出一个类来与子系统交互,隔离客户端与子系统之间的联系 ...
- Python学习---网络编程 1217【all】
OSI七层模型: 物理层, 数据链路层, 网络层,传输层,会话层,表达层,应用层 应用层:TFTP,HTTP,SNMP,FTP,SMTP,DNS,Telnet 等等 传输层:TCP,UDP 网络层:I ...
- 企业级Apache详解
安装Apache #Apache安装 rpm -qa|grep httpd yum install httpd #2编译安装: -->推荐安装 cd /root/software yum -y ...
- Asp.Net MVC Identity 2.2.1 使用技巧(一)
开发环境:vs2015 UP3 or vs2017RC 项目环境:asp.net 4.6.1 identity版本为:asp.net identity 2.2.1 1.创建项目. 没什么好说 ...
- python 实现插入排序、冒泡排序、归并排序
def InsertSort(A): '''插入排序算法:传入一个list,对list中的数字进行排序''' print('插入排序前list元素顺序:',A) length=len(A) for i ...
- Redis数据的底层存储原理
redis底层是用什么结构来存储数据的呢? 我们从源码上去理解就会容易的多: redis底层是使用C语言来编写的,我们可以看到它的数据结构声明.一个 dict 有两个dictht,一个dictht ...
- WebDAV漏洞直接远程溢出拿下服务器
以上是 我做的幻灯片 介绍什么是WebDAV 有什么用 漏洞测试需要的工具 http://pan.baidu.com/s/1o6DYxAE看到这里 说明直接远程溢出 提升权限为system 这些都很久 ...