#C297. Sudoku Puzzle Solver

    ID: 46344 Type: Default 1000ms 256MiB

Sudoku Puzzle Solver

Sudoku Puzzle Solver

Your task is to complete a given Sudoku puzzle. The puzzle is represented by a 9 × 9 grid, where each cell contains an integer from 0 to 9. A value of 0 indicates an empty cell. You must fill the blank cells so that every row, every column, and every 3 × 3 subgrid contains all the digits from 1 to 9 exactly once.

More formally, if we denote the board as \(A_{ij}\) where \(i,j\) are the row and column indices respectively, then the following conditions should hold for a valid solution:

  • Each row: \(\{A_{i1}, A_{i2}, \dots, A_{i9}\} = \{1, 2, \dots, 9\}\), for \(1 \le i \le 9\).
  • Each column: \(\{A_{1j}, A_{2j}, \dots, A_{9j}\} = \{1, 2, \dots, 9\}\), for \(1 \le j \le 9\).
  • Each 3 × 3 subgrid (for example, the one starting at \(A_{1,1}\)): it must contain all digits from 1 to 9.

You are required to implement a backtracking algorithm to solve the Sudoku puzzle.

inputFormat

The input consists of 9 lines. Each line contains 9 space-separated integers representing a row of the Sudoku puzzle. A 0 represents an empty cell.

For example:

5 3 0 0 7 0 0 0 0
6 0 0 1 9 5 0 0 0
0 9 8 0 0 0 0 6 0
8 0 0 0 6 0 0 0 3
4 0 0 8 0 3 0 0 1
7 0 0 0 2 0 0 0 6
0 6 0 0 0 0 2 8 0
0 0 0 4 1 9 0 0 5
0 0 0 0 8 0 0 7 9

outputFormat

Output the solved Sudoku puzzle as 9 lines, each containing 9 space-separated integers. The solution must be printed to stdout.

## sample
5 3 0 0 7 0 0 0 0
6 0 0 1 9 5 0 0 0
0 9 8 0 0 0 0 6 0
8 0 0 0 6 0 0 0 3
4 0 0 8 0 3 0 0 1
7 0 0 0 2 0 0 0 6
0 6 0 0 0 0 2 8 0
0 0 0 4 1 9 0 0 5
0 0 0 0 8 0 0 7 9
5 3 4 6 7 8 9 1 2

6 7 2 1 9 5 3 4 8 1 9 8 3 4 2 5 6 7 8 5 9 7 6 1 4 2 3 4 2 6 8 5 3 7 9 1 7 1 3 9 2 4 8 5 6 9 6 1 5 3 7 2 8 4 2 8 7 4 1 9 6 3 5 3 4 5 2 8 6 1 7 9

</p>