Title Description
Minesweeper is a very classic single player game. In the minefields of row n and row m, some grids contain mines (called mine grids), while others do not contain mines (called non mine grids). When the player opens a non mine cell, a number will appear in the cell - indicating how many of the surrounding cells are mine cells. The goal of the game is to find out all the non mine cells without turning out any mine cells.
Now we give the distribution of mines in the minefields of n rows and m columns, and we need to calculate the number of mines around each non mine grid.
Note: the surrounding grid of a grid includes the grid directly adjacent to it in eight directions: top, bottom, left, right, top left, top right, bottom left and bottom right.
For 100% data, 1 ≤ n ≤ 100, 1 ≤ m ≤ 100.
input
Input format:
The first line of the input file is two integers n and m separated by a space, which respectively represent the number of lines and columns in the minefield.
Next n lines, m characters per line, describe the distribution of mines in the minefield. The character '*' indicates that the corresponding cell is a mine cell, and the character '?' indicates that the corresponding cell is a non mine cell. There is no separator between adjacent characters.
output
Output format:
The output file contains n lines, m characters per line, describing the whole minefield. Use "*" to represent the mine cell, and use the number of mines around to represent the non mine cell. There is no separator between adjacent characters.
sample input
3 3
*??
???
?*?
sample output
*10
221
1*1
thinking
1. Determine whether it is thunder first
2. If it's not a thunder, search whether the peripheral circle is a thunder and record the data
3. Output in sequence
Code:
#include <bits/stdc++.h> using namespace std; int main() { int n,m; cin>>n>>m; char a[105][105]; for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) cin>>a[i][j]; for(int i=1;i<=n;i++) { for(int j=1;j<=m;j++) { int s=0; if(a[i][j]=='*')//Judge whether it is thunder cout<<a[i][j]; else//If it's thunder, traverse around { if(a[i-1][j]=='*'&&i>=2) s++; if(a[i+1][j]=='*'&&i<=n-1) s++; if(a[i][j-1]=='*'&&j>=2) s++; if(a[i][j+1]=='*'&&j<=m-1) s++; if(a[i-1][j-1]=='*'&&i>=2&&j>=2) s++; if(a[i+1][j-1]=='*'&&i<=n-1&&j>=2) s++; if(a[i-1][j+1]=='*'&&i>=2&&j<=m-1) s++; if(a[i+1][j+1]=='*'&&i<=n-1&&j<=m-1) s++; cout<<s;//Output lightning number } } cout<<endl; }
}