#K68592. Sudoku Solver

    ID: 32898 Type: Default 1000ms 256MiB

Sudoku Solver

Sudoku Solver

You are given a partially filled \(9 \times 9\) Sudoku board. Write a program to fill the board so that every row, every column, and every \(3 \times 3\) sub-box contains the digits from 1 to 9 exactly once. Empty cells are denoted by a dot (.).

The Sudoku rules are defined as follows:

  • Each digit from 1 to 9 must occur exactly once in each row.
  • Each digit from 1 to 9 must occur exactly once in each column.
  • Each digit from 1 to 9 must occur exactly once in each of the nine \(3 \times 3\) sub-boxes, where the sub-boxes are defined by \( \begin{array}{ccc} (1,1) & (1,2) & (1,3) \\ (2,1) & (2,2) & (2,3) \\ (3,1) & (3,2) & (3,3) \end{array} \).

Your program must read the puzzle from standard input and output the solved puzzle to standard output in the same format.

inputFormat

The input consists of 9 lines representing the Sudoku board. Each line contains 9 characters separated by spaces. The digits '1' to '9' represent filled cells and a dot (.) represents an empty cell.

For example:

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

outputFormat

Output the solved Sudoku board in the same format as the input: 9 lines with 9 digits each separated by a space. If the input puzzle is already solved, output the original board.

For example:

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
## sample
5 3 . . 7 . . . .
6 . . 1 9 5 . . .
. 9 8 . . . . 6 .
8 . . . 6 . . . 3
4 . . 8 . 3 . . 1
7 . . . 2 . . . 6
. 6 . . . . 2 8 .
. . . 4 1 9 . . 5
. . . . 8 . . 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>