题目描述

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

输入

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$ \le$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.

输出

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.

样例输入

4 4
*...
....
.*..
....
3 5
**...
.....
.*...
0 0

样例输出

Field #1:
*100
2210
1*10
1110Field #2:
**100
33200
1*100

解题思路:本质上其实就是扫雷游戏,摒弃原有多层判断(四角、边缘、内部)。

换一种思路:既然是计算周围九宫格的雷数,就应该想到只要遍历对应元素周围(行差<=1 && 列差<=1)同时字符=='*'即可

参考代码:

#include<iostream>

#include<cmath>

#define maxn 105

using namespace std;

void printScreen(char (*result)[maxn], int m, int n, int num);

int main()

{

    int a,b;

    int num=0; 

    char mineWeeper[maxn][maxn]; 

    char mineResult[maxn][maxn];  //存储结果

    while(cin>>a>>b && a!=0 && b!=0){

        num++;

        for(int i=0; i<a; i++){

            for(int j=0; j<b; j++){

                cin >> mineWeeper[i][j];

            }

        }

        int y=0,x=0;

        while(y<a && x<b){

            int cnt=0; //雷数

            if(mineWeeper[y][x] == '*') mineResult[y][x] = '*';

            else{

                for(int i=0; i<a; i++){

                for(int j=0; j<b; j++){

                    if(abs(i-y)<=1 && abs(j-x)<=1 && mineWeeper[i][j]=='*') cnt++;

                }

                }

                mineResult[y][x] = cnt+'0';

            }

            if(x+1>=b){

                y++;

                x = 0;

            }else x++;

        }

        printScreen(mineResult, a, b, num);

    }

     

    return 0;

}

void printScreen(char (*result)[maxn], int m, int n, int num){

    cout << "Field #" << num << ":" <<endl;

    for(int i=0; i<m; i++){

        for(int j=0; j<n; j++){

            cout << result[i][j];

            if(j == n-1) cout<<'\n';

        }

    }

    cout<<'\n';

}

Minesweeper 蓝桥杯 扫雷游戏(化繁为简)(摒弃多层循环)相关推荐

  1. java 蓝桥杯 石子游戏(题解)

    试题 算法训练 石子游戏 资源限制 时间限制:1.0s 内存限制:256.0MB 问题描述 石子游戏的规则如下: 地上有n堆石子,每次操作可选取两堆石子(石子个数分别为x和y)并将它们合并,操作的得分 ...

  2. java 蓝桥杯 数字游戏

    试题 算法训练 数字游戏 资源限制 时间限制:1.0s 内存限制:256.0MB 问题描述 给定一个1-N的排列a[i],每次将相邻两个数相加,得到新序列,再对新序列重复这样的操作,显然每次得到的序列 ...

  3. 蓝桥杯 传球游戏 动态规划

    题目描述 上体育课的时候,小蛮的老师经常带着同学们一起做游戏.这次,老师带着同学们一起做传球游戏. 游戏规则是这样的:n个同学站成一个圆圈,其中的一个同学手里拿着一个球,当老师吹哨子时开始传球,每个同 ...

  4. 蓝桥杯 传球游戏 c++实现

    题目描述 上体育课的时候,小蛮的老师经常带着同学们一起做游戏.这次,老师带着同学们一起做传球游戏. 游戏规则是这样的:n个同学站成一个圆圈,其中的一个同学手里拿着一个球,当老师吹哨子时开始传球,每个同 ...

  5. 数独游戏完整java代码_Java实现蓝桥杯数独游戏的示例代码

    你一定听说过"数独"游戏. 如图,玩家需要根据9×9盘面上的已知数字,推理出所有剩余空格的数字,并满足每一行.每一列.每一个同色九宫内的数字均含1-9,不重复. 数独的答案都是唯一 ...

  6. 蓝桥杯 算法提高 转圈游戏 JAVA

    蓝桥杯 转圈游戏 原题描述 解题思路 JAVA代码 运行结果 原题描述 问题描述 n 个小伙伴(编号从 0 到 n-1)围坐一圈玩游戏.按照顺时针方向给 n 个位置编号,从0 到 n-1.最初,第 0 ...

  7. 2019蓝桥杯每周一题第二周之Mineweep(扫雷)

    2019蓝桥杯每周一题第二周之Mineweep(扫雷) 写在开头: 写这个题的时候真的是一次次的纠错,题不难,里面的逻辑关系有复杂,每一次都以为会运行正确了,结果又一个小地方出错,写了一上午还是有问题 ...

  8. 击鼓游戏-第10届蓝桥杯Scratch省赛真题第7题

    [导读]:超平老师计划推出Scratch蓝桥杯真题解析100讲,这是超平老师解读Scratch蓝桥真题系列的第62讲. 第10届蓝桥杯青少年组省赛于2019年3月24日举行,形式为线下考试.Scrat ...

  9. 九宫格游戏-第14届蓝桥杯省赛Scratch初级组真题第5题

    [导读]:超平老师的<Scratch蓝桥杯真题解析100讲>已经全部完成,后续会不定期解读蓝桥杯真题,这是Scratch蓝桥杯真题解析第134讲. 九宫格游戏,本题是2023年5月7日举行 ...

  10. python扫雷 广度优先_Leetcode之广度优先搜索(BFS)专题-529. 扫雷游戏(Minesweeper)...

    Leetcode之广度优先搜索(BFS)专题-529. 扫雷游戏(Minesweeper) BFS入门详解:Leetcode之广度优先搜索(BFS)专题-429. N叉树的层序遍历(N-ary Tre ...

最新文章

  1. js数组按照下标对象的属性排序
  2. 再谈编程范式-程序语言背后的思想
  3. JUC原子类-基本类型(二)
  4. MySQL修改字符集
  5. Linux环境下的JFreeChart中文乱码问题解决办法
  6. VBS脚本压缩IIS日志
  7. 双向链表list.h升序排序
  8. docker安装mysql以及设置navicat远程访问
  9. php 电梯程序设计,西门子300PLC编写三层电梯程序的设计与模拟
  10. 【js练习】简易聊天室
  11. Java实现图片水印
  12. 编写程序,统计某旅馆住宿客人的总数,要求输入客人姓名,输出客人编号(按先后顺序自动生成),姓名以及总人数。...
  13. 计算机中文件夹属性有哪些,文件和文件夹属性
  14. bgp状态idle什么原因_BGP - 2,BGP报文和BGP状态(转)
  15. ubuntu 20 无法联网或无法解析域名(2022最新办法,实测有效)
  16. 【数值分析】复化积分公式
  17. 金盘转债上市价格预测
  18. qca9535 tftp32 刷机_20151210编译高通的qca9531的wireless版本 修改版本4
  19. 计算机科学与技术职业生涯规划,计算机科学与技术职业生涯规划ppt
  20. 交通流预测爬坑记(一):交通流数据集,原始数据

热门文章

  1. unity3d 取锚点位置_加热炉传输点
  2. 迪杰斯特拉(Dijkstra)算法
  3. Haproxy配置应用文档
  4. 什么是美国能源之星计划?
  5. Pagehelper获取total错误解决方案
  6. VTK系列教程九:VR图像裁剪
  7. 全网无损音乐、超清视频免费下!
  8. 巧妙的实现一套键鼠无缝控制两台联网的主机
  9. 60个超实用的网络技能学习平台
  10. 数据结构队列的代码实现