Tudoku

Time limit1sMemory limit128 MB

Summary
Fill in each 9x9 Sudoku board: whenever a row, column, or 3x3 block has exactly one empty cell, that cell is forced, and repeating this finishes every board.
Level

Medium4 of 10

Topics
Simulation, Implementation, Array, Brute force
Solved
No attempts yet

Problem

Tom is a master of several mathematical disciplines. He recently founded a research lab at the university and teaches newcomers like Jim. In his first lesson he explained the game of Tudoku to Jim. Tudoku is a straightforward variant of Sudoku that starts from a board where almost every cell is already filled in — exactly the kind of board Tom leaves behind when he gives up on an ordinary Sudoku because he is too lazy to fill in the last few obvious cells. Help Jim solve every Tudoku that Tom leaves for him.

Sudoku is played on a 9 × 9 board divided into nine 3 × 3 blocks. Initially only a few cells contain numbers, and the goal is to fill the remaining cells so that every row, every column, and every 3 × 3 block contains each number from 1 to 9 exactly once.

Every Tudoku board can be solved by repeatedly applying the following rule: if some row, column, or 3 × 3 block already contains exactly eight numbers, the single empty cell in it is uniquely determined, so fill it in. Applying this rule repeatedly is guaranteed to complete every board in the input.

Input

The first line contains the number of scenarios. Each scenario consists of nine lines of nine digits each. A digit of 0 marks a cell that Tom has not filled in and that you must fill. Each scenario is followed by an empty line.

Output

For each scenario, first print a line of the form Scenario #i:, where i is the scenario number starting at 1. Then print the completed Tudoku board in the same format as the input, with every 0 replaced by the correct digit, as nine lines. Terminate the output for each scenario with a blank line.

Examples3

  1. Example 1

    Input
    2
    000000000
    817965430
    652743190
    175439820
    308102950
    294856370
    581697240
    903504610
    746321580
    
    781654392
    962837154
    543219786
    439182675
    158976423
    627543918
    316728549
    895461237
    274395861
    
    Expected output
    Scenario #1:
    439218765
    817965432
    652743198
    175439826
    368172954
    294856371
    581697243
    923584617
    746321589
    
    Scenario #2:
    781654392
    962837154
    543219786
    439182675
    158976423
    627543918
    316728549
    895461237
    274395861
    
  2. Example 2

    Input
    1
    936875214
    857412369
    421963578
    795128436
    643759821
    182634957
    314596782
    278341695
    569287143
    
    Expected output
    Scenario #1:
    936875214
    857412369
    421963578
    795128436
    643759821
    182634957
    314596782
    278341695
    569287143
    
  3. Example 3

    Input
    1
    728156394
    439728561
    156439287
    645973128
    973812456
    812645739
    261594873
    594387612
    387261940
    
    Expected output
    Scenario #1:
    728156394
    439728561
    156439287
    645973128
    973812456
    812645739
    261594873
    594387612
    387261945