[NOIP2015 Popularity Group] Minesweeper Game

Topic link

Title description
Minesweeper game is a very classic stand-alone game. In the minefields of n rows and m columns, some grids contain mines (called landmines), and other grids do not contain mines (called non-landmines). When the player turns on a non-land mine grid, a number will appear in that grid—a hint how many of the surrounding grids are mine grids. The goal of the game is to find all non-land mines without revealing any mines.

Now given the distribution of mines in a minefield with n rows and m columns, it is required to calculate the number of mine grids around each non-land mine grid.

Note: The surrounding grids of a grid include the grids directly adjacent to it in the eight directions of its top, bottom, left, right, top left, top right, bottom left, and bottom right.

Input format The
first line is two integers n and m separated by a space, indicating the number of rows and columns of the minefield respectively.

The next n lines, each with m characters, describe the distribution of mines in the minefield. The character'*' indicates that the corresponding grid is mine, and the character'?' indicates that the corresponding grid is non- mine. There is no separator between adjacent characters.

Output format The
output file contains n lines with m characters per line, describing the entire minefield. Use'*' to indicate mines, and use the number of surrounding mines to indicate non-land mines. There is no separator between adjacent characters.

Input and output sample
input #1

3 3
*??
???
?*?

Output #1

*10
221
1*1

Input #2

2 3
?*?
*??

Output #2

2*1
*21

Note/tips
For 100% data, 1≤n≤100, 1≤m≤100.

Code:

//P2670 扫雷游戏
#include<iostream>
using namespace std;
char ma[110][110];
int main()
{
    
    
	int n, m;
	cin >> n >> m;
	for(int i = 1; i <= n; i++)
		for(int j = 1; j <= m; j++)
			cin >> ma[i][j];
	for(int i = 1; i <= n; i++)
	{
    
    
		for(int j = 1; j <= m; j++)
		{
    
    
			if(ma[i][j] == '*') cout << "*";
			else
			{
    
    
				int count = 0;
				for(int p = i - 1; p <= i + 1; p++)
					for(int q = j - 1; q <= j + 1; q++)
						if(ma[p][q] == '*') count++;
				cout << count;
			}
		}
		cout << endl;
	}
	return 0;
}

Guess you like

Origin blog.csdn.net/qq_44826711/article/details/113748200