Sudoku
Time limit1sMemory limit128 MB
Fill the five empty cells (marked 0) in a 9x9 Sudoku grid so every row, column, and 3x3 box contains the digits 1 through 9 exactly once.
- Level
Medium5 of 10
- Topics
- Backtracking, Implementation, Brute force, Recursion
- Solved
- No attempts yet
Problem
Sudoku is played on a grid that is divided into nine boxes. You must fill the grid with the digits through so that every digit appears exactly once in each row, exactly once in each column, and exactly once in each box.
A puzzle is given as a grid in which a filled cell holds its digit and an empty cell holds . Given such a puzzle, write a program that solves it.
Input
The first line contains the number of test cases. Each test case consists of lines describing a Sudoku grid; each line is a string of digits from to , where marks an empty cell. Every test case contains exactly five s.
Output
If the grid cannot be completed without breaking the Sudoku rules, print
Could not complete this grid.
Otherwise, print the solved grid over lines using the same format as the input. Print a blank line between consecutive test cases. Whenever a grid can be solved, the input is guaranteed to have a unique solution.