NOIP2015 Minesweeper Game

Question H: Minesweeper Game

Time Limit:  1 Sec   Memory Limit:  128 MB
Commits:  228   Resolved:  132
[ Commit ][ Status ][ Discussion Board ][Assert By: admin ]

Topic description

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

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

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

enter

The first line is two integers n and m (1≤n≤100, 1≤m≤100) separated by a space, representing 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 a land grid, and the character '?' indicates that the corresponding grid is a non-land grid. There are no separators between adjacent characters.

output

The output contains n lines of m characters each describing the entire minefield. Use '*' to represent mines, and use the number of surrounding mines to represent non- mines. There are no separators between adjacent characters.

sample input

3 3

*??

???

?*?

Sample output

*10

221

1*1


#include<iostream>
#include<cstring>
using namespace std;
char a[105][105];
int hang[9] = {1,1,1,0,0,-1,-1,-1}; //八个方向
int lie[9]  = {0,-1,1,-1,1,0,-1,1};
int main()
{
    int m, n;
    while(cin >> n >> m)
    {
        memset(a, '0', sizeof(a));
        for(int i=1; i<=n; i++)
            for(int j=1; j<=m; j++)
            {
                char ch;
                cin >> ch;
                if(ch == '*')
                    a[i][j] = ch;
            }
        for(int i=1; i<=n; i++)
            for(int j=1; j<=m; j++)
                if(a[i][j] == '*')
                    for(int k=0; k<9; k++)
                        if(a[i+hang[k]][j+lie[k]]!='*')
                            a[i+hang[k]][j+lie[k]] = (char)(a[i+hang[k]][j+lie[k]]+1);
        for(int i=1; i<=n; i++)
            for(int j=1; j<=m; j++)
            {
                cout << a[i][j];
                if(j == m) cout << endl;
            }
    }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325844262&siteId=291194637