Minesweeper Master

Time limit5sMemory limit512 MB

Summary
You place M mines on an R by C board so a single corner click reveals every safe cell, or report that no such board exists.
Level

Medium7 of 10

Topics
Implementation, Matrix
Solved
No attempts yet

Problem

Minesweeper is a computer game that became popular in the 1980s, and some versions of Microsoft Windows still include it. This problem uses the same rules, but you can solve it without ever having played the game.

You play on an R×CR \times C grid. Every cell is hidden at the start. One mine is hidden in each of MM different cells, and no other cell holds a mine. You may click any cell to reveal it. If the revealed cell holds a mine, the game ends and you lose. Otherwise the revealed cell shows a digit between 0 and 8, the number of neighboring cells that hold a mine. Two cells are neighbors when they share an edge or a corner. If a revealed cell shows 0, all of its neighbors are revealed automatically, and the same happens again for every newly revealed cell that shows 0. When every cell without a mine has been revealed, the game ends and you win.

For example, suppose the board starts like this, where '*' is a mine and 'c' is the clicked cell.

* . . * . . . * * .
. . . . * . . . . .
. . c . . * . . . .
. . . . . . . . * .
. . . . . . . . . .

The clicked cell has no neighboring mine, so it shows 0 and its eight neighbors are revealed with it. The reveal keeps spreading, and the board becomes this.

* . . * . . . * * .
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 . .

Cells that hold no mine are still hidden (the '.' characters), so you have to click again to continue the game.

Nothing is quicker than winning in one click. Given the board size R×CR \times C and the number of mines MM, and given that you choose which cell to click, decide whether some arrangement lets you win in one click, and print one such arrangement when it exists.

Input

The first line contains the number of test cases TT. Each of the next TT lines contains three space separated integers RR, CC, and MM.

Limits

  • 1≤T≤1401 \le T \le 140
  • 1≤R,C≤501 \le R, C \le 50
  • 0≤M<R×C0 \le M < R \times C

Output

For each test case, first print a line of the form "Case #x:", where xx is the test case number starting from 1.

If no arrangement wins in one click, print Impossible on the next line.

Otherwise print RR lines of CC characters each. Write '.' for a cell without a mine, '*' for a cell with a mine, and 'c' for the clicked cell. Several arrangements can win in one click, so print the one that meets all four conditions below.

  1. The clicked cell is the cell in the first row and the first column.
  2. In every row the cells without a mine run from the first column with no gap. The clicked cell counts as a cell without a mine.
  3. The number of cells without a mine never grows as you move down the rows.
  4. Among the arrangements that win in one click and meet the first three conditions, print the one whose sequence (a1,a2,…,aR)(a_1, a_2, \dots, a_R) is largest in lexicographic order, where aia_i is the number of cells without a mine in row ii.

Whenever some arrangement wins in one click, an arrangement meeting all four conditions also exists.

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
    6
    1 1 0
    1 6 0
    1 6 5
    6 1 4
    2 2 0
    2 2 2
    
    Expected output
    Case #1:
    c
    Case #2:
    c.....
    Case #3:
    c*****
    Case #4:
    c
    .
    *
    *
    *
    *
    Case #5:
    c.
    ..
    Case #6:
    Impossible