Minesweeper
Time limit1sMemory limit128 MB
Given an n×n mine layout and which cells are currently opened, print the board: mine counts for opened safe cells, asterisks for all mines if any mine was opened, and dots elsewhere.
- Level
Easy3 of 10
- Topics
- Simulation, Implementation, Array
- Solved
- No attempts yet
Problem
Minesweeper is played on an grid. mines are hidden, one per cell, on distinct cells.
The player repeatedly picks a cell and opens it.
- If the player opens a cell that contains a mine, the player loses.
- If the player opens a cell with no mine, that cell shows a number between 0 and 8: the count of mines among the 8 cells adjacent to it horizontally, vertically, or diagonally.
The player wins by opening every safe cell until only the mined cells remain unopened. Those remaining cells must all be mines.
Your task is to read the state of a partially played game and print the corresponding current state of the grid.
Input
The first line contains a positive integer , the size of the grid ().
The next lines describe the mine layout. Each line represents one row using characters: a period (.) is a cell with no mine, and an asterisk (*) is a cell with a mine.
The following lines each contain a string of length describing which cells have been opened. A lowercase x marks an already-opened cell, and a period (.) marks a cell that has not been opened.
Output
Print the current state of the grid on lines.
- For each opened cell that contains no mine, print the number of adjacent mines (between 0 and 8).
- If at least one mined cell has been opened, print every mined cell as an asterisk (
*). - Print every other cell as a period (
.).