Minesweeper Master
Time limit5sMemory limit512 MB
Place M mines on an R by C grid so one click on the top-left cell reveals every mine-free cell, or report that no such board exists.
- Level
Medium7 of 10
- Topics
- Implementation, Simulation
- Solved
- No attempts yet
Problem
Minesweeper is played on a grid of rows and columns. Every cell starts hidden. of the cells hold a mine and the other cells hold nothing.
You click one cell to reveal it. If that cell holds a mine, you lose. Otherwise the cell shows a digit from 0 to 8, the number of neighboring cells that hold a mine. Two cells are neighbors when they share an edge or a corner. When a revealed cell shows 0, all of its neighbors are revealed too, and the same rule applies again to every newly revealed cell that shows 0. You win once every cell without a mine is revealed.
Here is a board before the first click. * is a mine and c is the cell you click.
* . . * . . . * * .
. . . . * . . . . .
. . c . . * . . . .
. . . . . . . . * .
. . . . . . . . . .
No mine touches the clicked cell, so it shows 0, its 8 neighbors are revealed, and the reveal keeps spreading:
* . . * . . . * * .
1 1 1 2 * . . . . .
0 0 0 1 2 * . . . .
0 0 0 0 1 1 1 1 * .
0 0 0 0 0 0 0 1 . .
The cells still drawn as . hold no mine and are still hidden, so this click does not win the game.
You want to win with a single click. Given , and , place the mines and choose the cell to click so that the first click wins, or report that no such board exists.
Input
The first line has the number of test cases . Each of the next lines has three integers , and separated by spaces.
Limits:
Output
For each test case, print a line Case #x:, where x is the test case number starting from 1. On the next lines print the board, characters per line, using . for a cell without a mine, * for a mine, and c for the clicked cell.
Many different boards can win with one click, so print exactly the board these rules pick.
- The clicked cell is the cell in row 1, column 1.
- In every row the cells without a mine fill the left end of that row: if row holds cells without a mine, they are the cells in columns 1 through , and . The clicked cell is a cell without a mine, so .
- Among the boards of this shape that win with one click, print the one whose sequence is the greatest in lexicographic order. Make as large as possible, then , and so on.
If no board of this shape wins with one click, print Impossible on a single line instead of the board.