烟大 Contest1024 - 《挑战编程》第一章:入门 Problem B: Minesweeper(模拟扫雷)
Problem B: Minesweeper
Time Limit: 1 Sec Memory Limit: 64 MB
Submit: 29 Solved: 7
[Submit][Status][Web Board]
Description
Minesweeper Have you ever played Minesweeper? This cute little game comes with a certain operating system whose name we can't remember. The goal of the game is to find where all the mines are located within a M x N field. The game shows a number in a square which tells you how many mines there are adjacent to that square. Each square has at most eight adjacent squares. The 4 x 4 field on the left contains two mines, each represented by a ``*'' character. If we represent the same field by the hint numbers described above, we end up with the field on the right: *... .... .*.. .... *100 2210 1*10 1110
Input
The input will consist of an arbitrary number of fields. The first line of each field contains two integers n and m ( 0 < n, m<100) which stand for the number of lines and columns of the field, respectively. Each of the next n lines contains exactly m characters, representing the field. Safe squares are denoted by ``.'' and mine squares by ``*,'' both without the quotes. The first field line where n = m = 0 represents the end of input and should not be processed.
Output
For each field, print the message Field #x: on a line alone, where x stands for the number of the field starting from 1. The next n lines should contain the field with the ``.'' characters replaced by the number of mines adjacent to that square. There must be an empty line between field outputs.
Sample Input
Sample Output
HINT
模拟题。模拟的是扫雷这个小游戏。输入雷的分布,输出每一个格子四周的雷数矩阵。也就是模拟扫雷里数字的产生过程。
一开始把计算那一步想的复杂了,由于没有初始化,我只好把检测的情况分为边缘、内部,边缘又分为四角和边,这样有三种情况,每一种情况的检测区域又不一样,我要每一种都分别定义。后来发现没必要分类,直接检测周围8个格子的区域就行,只不过这样就需要提前初始化好以及让数组留出边缘。
My code:
#include <iostream> using namespace std; int main()
{
char a[][]={};
int n,m,count=;
while(cin>>n>>m){
if(n== && m==)
break;
for(int i=;i<=n;i++) //input
for(int j=;j<=m;j++)
cin>>a[i][j];
for(int i=;i<=n;i++) //计算
for(int j=;j<=m;j++){
if(a[i][j]=='*')
continue; a[i][j]='';
if(a[i-][j]=='*')
++a[i][j];
if(a[i+][j]=='*')
++a[i][j];
if(a[i][j+]=='*')
++a[i][j];
if(a[i][j-]=='*')
++a[i][j]; if(a[i-][j-]=='*')
++a[i][j];
if(a[i-][j+]=='*')
++a[i][j];
if(a[i+][j-]=='*')
++a[i][j];
if(a[i+][j+]=='*')
++a[i][j];
} if(count!=)
cout<<endl;
cout<<"Field #"<<count++<<':'<<endl;
for(int i=;i<=n;i++){ //输出
for(int j=;j<=m;j++){
cout<<a[i][j];
}
cout<<endl;
}
}
return ;
} /**************************************************************
Problem: 1099
User: freecode
Language: C++
Result: Accepted
Time:0 ms
Memory:1268 kb
****************************************************************/
Freecode : www.cnblogs.com/yym2013
烟大 Contest1024 - 《挑战编程》第一章:入门 Problem B: Minesweeper(模拟扫雷)的更多相关文章
- ACM YTU 《挑战编程》第一章 入门 Problem E: Graphical Editor
Description Graphical editors such as Photoshop allow us to alter bit-mapped images in the same way ...
- Windows核心编程第一章.错误处理
Windows核心编程第一章,错误处理. 一丶错误处理 1.核心编程学习总结 不管是做逆向,开始做开发.在Windows下.你都需要看一下核心编程这本书.这本书确实写得很好.所以自己在学习这本书的同时 ...
- .NET Core实战项目之CMS 第一章 入门篇-开篇及总体规划
作者:依乐祝 原文地址:https://www.cnblogs.com/yilezhu/p/9977862.html 写在前面 千呼万唤始出来,首先,请允许我长吸一口气!真没想到一份来自28岁老程序员 ...
- net core体系-web应用程序-4asp.net core2.0 项目实战(CMS)-第一章 入门篇-开篇及总体规划
.NET Core实战项目之CMS 第一章 入门篇-开篇及总体规划 原文地址:https://www.cnblogs.com/yilezhu/p/9977862.html 写在前面 千呼万唤始出来 ...
- Java 面向对象编程——第一章 初识Java
第一章 初识Java 1. 什么是Java? Java是一种简单的.面向对象的.分布式的.解释的.安全的.可移植的.性能优异的多线程语言.它以其强安全性.平台无关性.硬件结构无关性.语言简 ...
- Java学习笔记 第一章 入门<转>
第一章 JAVA入门 一.基础常识 1.软件开发 什么是软件? 软件:一系列按照特定顺序组织的计算机数据和指令的集合 系统软件:DOS,Windows,Linux 应用软件:扫雷.QQ.迅雷 什么是开 ...
- windows核心编程-第一章 对程序错误的处理
第一章-对程序错误的处理 在开始介绍Microsoft Windows 的特性之前,必须首先了解 Wi n d o w s的各个函数是如何进行错误处理的. 当调用一个Wi n d o w s函数时,它 ...
- UNIX环境高级编程--第一章 UNIX基础知识
第一章 UNIX基础知识 1.2 UNIX体系结构 从严格意义上说,可将操作系统定义为一种软件,它控制计算机硬件资源,提供程序运行环境.我们将这种软件称为内核(kernel),因为 它相对较小,且 ...
- 读高性能JavaScript编程 第一章
草草的看完第一章,虽然看的是译文也是感觉涨姿势了, 我来总结一下: 由于 大多数浏览器都是 single process 处理 ui updatas and js execute 于是产生问题: js ...
- Go Web 编程 第一章 Web相关概念
第一章 Go与Web应用 Go学习群:415660935 1.1 Web应用 在计算机的世界里,应用(application)是一个与用户进行交互,并完成用户特定任务的软件程序.而Web应用则是部署在 ...
随机推荐
- std::shared_ptr
在std::shared_ptr被引入之前,C++标准库中实现的用于管理资源的智能指针只有std::auto_ptr一个而已.std::auto_ptr的作用非常有限,因为它存在被管理资源的所有权转移 ...
- 一种透明效果的view
设置这个view背景色: [UIColor colorWithRed: green: blue: alpha:0.3]; 效果如下:
- jquery 使用方法<转载>
jquery 使用方法 jQuery是目前使用最广泛的javascript函数库.据统计,全世界排名前100万的网站,有46%使用jQuery,远远超过其他库.微软公司 甚至把jQuery作为他们的官 ...
- 如何使用vim的帮助功能
set guioptions+-=m/T 更换vim的默认color schema: 将下载的color.vim文件如sonofob'sidian.vim放到/usr/share/vim/vimfil ...
- sudo: unable to resolve host ubuntu提示的解决
http://blog.sina.com.cn/s/blog_6c9d65a1010180mg.html
- 模板插件aTpl.js新增功能
摘要: aTpl.js是一款模板插件,该插件支持ie5+,chrome等浏览器以及移动端浏览器,支持for和if语法,以及表达式.最近对aTpl.js模板插件增加了新的功能,支持字符串模板,同时增加了 ...
- 聊下并发和Tomcat线程数(Updated)
最近一直在解决线上一个问题,表现是: Tomcat每到凌晨会有一个高峰,峰值的并发达到了3000以上,最后的结果是Tomcat线程池满了,日志看很多请求超过了1s. 服务器性能很好,Tomcat版本是 ...
- Asteroids(匈牙利算法入门)
Asteroids Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16211 Accepted: 8819 Descri ...
- Ubuntu 开机自启动工具 update-rd.d 使用详解
常用命令: $ sudo update-rc.d nginx defaults #增加服务 $ sudo update-rc.d -f nginx remove #移除服务 Linux ...
- PHP导出数据到CSV文件函数/方法
如果不清楚什么是CSV文件,可看如下文章介绍 CSV格式的是什么文件?CSV是什么的缩写? /** * 导出数据到CSV文件 * @param array $data 数据 * @param arr ...