Under construction

Still being built. Things may change

Drop7

Time limit1sMemory limit128 MB

Summary
Simulate n disc drops on a 7 by 7 board with simultaneous number-match eliminations and gravity, then print the final board.
Level

Medium5 of 10

Topics
Simulation, Matrix
Solved
No attempts yet

Problem

Drop7 is a single player game on a 7×77 \times 7 vertical grid. The grid starts empty. On each turn you are handed a disc with a number from 1 to 7 written on it, and you drop it into one of the seven columns from above. The disc falls down the chosen column and stops on top of the highest disc already in that column. If the column is empty, the disc reaches the floor and stays in the bottom row.

After the drop, every disc whose number equals the size of its group column or the size of its group row disappears. All of them fade at the same moment. Then every disc that lost the disc under it falls straight down as far as it can. The condition is checked again over all remaining discs, and the loop repeats until no disc matches. Once nothing else disappears, the next turn begins.

The group column of a disc is the number of discs stacked in the same column, counting the disc itself. Say a column holds 7 and 7 from the bottom and you drop a 3 on top: the column now holds three discs, so the 3 disappears and the two 7s stay. One drop can also start a chain. Say a column holds 5, 5, 4 from the bottom and you drop a 3 on top. The column size is 4, so the 4 disappears first. The 3 then falls onto the two 5s, the column size becomes 3, and the 3 disappears as well.

The group row of a disc is the number of discs that sit side by side with no gap in the same row, counting the disc itself. If a row holds a 3 next to another 3 and a disc lands in the cell beside them so that three cells run together, both 3s have group row size 3 and they disappear at once.

You start from an empty board and receive the drops of one game. Simulate the game and print the final state of the board.

Input

The input holds several test cases. The first line of each test case holds nn (1≤n≤10001 \le n \le 1000), the number of discs dropped. Each of the next nn lines holds two integers viv_i and cic_i (1≤vi,ci≤71 \le v_i, c_i \le 7), meaning that on turn ii the disc numbered viv_i is dropped into column cic_i. Columns are numbered 1 to 7 from the left. The line after the last test case holds a single 0.

Output

For each test case print seven lines of seven characters. The first line is the top row of the grid and the seventh line is the bottom row. A cell holding a disc prints that disc's number, and an empty cell prints a hash sign (#).

If a disc is dropped into a column that already holds seven discs, the test case prints the single line Game Over! instead of the grid. The remaining drops of that test case are read but not played.

Print one blank line between the outputs of two consecutive test cases. Do not print a blank line after the last test case.

Examples2

  1. Example 1

    Input
    6
    2 2
    3 2
    6 2
    4 3
    1 1
    6 7
    11
    5 3
    5 3
    2 3
    2 3
    2 3
    3 3
    4 3
    5 3
    6 3
    7 3
    1 3
    0
    
    Expected output
    #######
    #######
    #######
    #######
    #######
    #######
    #64###6
    
    Game Over!
    
  2. Example 2

    Input
    4
    5 1
    5 1
    4 1
    3 1
    0
    
    Expected output
    #######
    #######
    #######
    #######
    #######
    5######
    5######