原题链接:http://www.dotcpp.com/oj/problem1096.html
题目描述
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 <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
1110
Field #2:
++100
33200
1+100
解题思路
这里注意一下,由于编辑器的问题,我把星号和点用加号和句号替代了,希望能理解,也能看一下链接的,那是原题(未改动的)
- 这里主要是英文的理解问题,可以通过使用翻译工具(针对英语菜鸡,比如我)大致理解一下题意:
- 你应该有玩过windows里的一个小游戏叫做"挖地雷"。这个游戏的目的就是要在M*N的地雷区格子中找出所有的地雷。
为了要帮助你,这个游戏会在非地雷的格子上有些数字,告诉你这一个格子的邻居共有多少个地雷。 - 所以这道题就是通过输入的点 输出每个点周围有多少个地雷(是地雷就原样输出地雷),每一个点,除了周围的,均有8个邻居,你就需要判断这八个邻居有多少个是地雷,周围的可以使用条件判断一下,也就可以了,我在这里主要是:
- 通过给定的点,给予给定坐标在-1到1之间进行加减,从而达到遍历周围的八个点,那么具体看代码吧
参考代码(c++描述)
#include<iostream>
#include<cstdio>
using namespace std;
char a[101][101];
int c(int dx, int dy, char a[][101]);
int n, m;
int main()
{
cin >> n >> m;
getchar();
int t = 0, k,b = 1;
while (n != 0 || m != 0)
{
t = 0;
//输入操作
for (int i = 0; i < n; ++i)
{
for (int j = 0; j < m; ++j)
{
cin >> a[i][j];
}
}
cout << "Field #" << b << ":" << endl;
for (int i = 0; i < n; ++i)
{
for (int j = 0; j < m; ++j)
{
if (a[i][j] == '*')
{
cout << '*';
}
else
{
k = c(i, j, a);
cout << k;
}
++t;
if (t % m == 0)
{
cout << endl;
}
}
}
cout << endl;
cin >> n >> m;
getchar();
++b;
}
return 0;
}
int c(int dx, int dy, char a[][101])
{
int count = 0;
int p = 0, q = 0;
for (int i = -1; i <= 1; ++i)
{
for (int j = -1; j <= 1; ++j)
{
p = dx + i;
q = dy + j;
if (p < 0 || p >= n || q < 0 || q >= m)
{
continue;
}
else
{
if (a[p][q] == '*')
{
++count;
}
}
}
}
return count;
}
核心算法:https://blog.csdn.net/weixin_42792088/article/details/87907514