Minesweeper Master

Time limit5sMemory limit512 MB

Summary
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 RR rows and CC columns. Every cell starts hidden. MM 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 RR, CC and MM, place the MM 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 TT. Each of the next TT lines has three integers RR, CC and MM separated by spaces.

Limits:

  • 1≤T≤2301 \le T \le 230
  • 1≤R,C≤101 \le R, C \le 10
  • 0≤M<R×C0 \le M < R \times C

Output

For each test case, print a line Case #x:, where x is the test case number starting from 1. On the next RR lines print the board, CC 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 ii holds aia_i cells without a mine, they are the cells in columns 1 through aia_i, and a1≥a2≥⋯≥aRa_1 \ge a_2 \ge \dots \ge a_R. The clicked cell is a cell without a mine, so a1+a2+⋯+aR=R×C−Ma_1 + a_2 + \dots + a_R = R \times C - M.
  • Among the boards of this shape that win with one click, print the one whose sequence (a1,a2,…,aR)(a_1, a_2, \dots, a_R) is the greatest in lexicographic order. Make a1a_1 as large as possible, then a2a_2, and so on.

If no board of this shape wins with one click, print Impossible on a single line instead of the board.

Examples2

  1. Example 1

    Input
    5
    5 5 23
    3 1 1
    2 2 1
    4 7 3
    10 10 82
    
    Expected output
    Case #1:
    Impossible
    Case #2:
    c
    .
    *
    Case #3:
    Impossible
    Case #4:
    c......
    .......
    .......
    ....***
    Case #5:
    c........*
    .........*
    **********
    **********
    **********
    **********
    **********
    **********
    **********
    **********
    
  2. Example 2

    Input
    4
    1 1 0
    1 7 6
    6 1 3
    5 5 24
    
    Expected output
    Case #1:
    c
    Case #2:
    c******
    Case #3:
    c
    .
    .
    *
    *
    *
    Case #4:
    c****
    *****
    *****
    *****
    *****