All problems

Problem 288

Sudoku Solver

Hard · 30 pts · Matrix, Backtracking, Hash Table

Problem

Solve a Sudoku puzzle. The 9 x 9 grid is divided into nine 3 x 3 boxes. Fill every empty cell ('.') with a digit from 1 to 9 so that each row, each column and each 3 x 3 box contains every digit exactly once. The given digits stay as they are, and the puzzle has exactly one solution. Use backtracking: find an empty cell, try every digit that does not already appear in its row, column or box, and recurse. If no digit works, undo the last choice. Choosing the empty cell with the fewest possible digits first makes the search much faster.

Input

9 lines of 9 characters each: digits and '.' for empty cells.

Output

Print the solved grid as 9 lines of 9 digits.

Constraints

The puzzle is valid and has a unique solution.

Example input

53..7....
6..195...
.98....6.
8...6...3
4..8.3..1
7...2...6
.6....28.
...419..5
....8..79

Example output

534678912
672195348
198342567
859761423
426853791
713924856
961537284
287419635
345286179

Explanation

Every empty cell gets the only digit that keeps all rows, columns and boxes valid. For example, the first row 53..7.... is completed to 534678912, with each digit appearing exactly once.

Every problem here can be used in a RelayTrixCode team match. Create a room.

Previous: Word Search Next: Combination Sum