Sudoku 3
Time limit1sMemory limit512 MB
Fill the empty cells of an n^2 by n^2 Sudoku board with values from 1 to n^2 so that rows, columns, and sub-grids stay distinct, filling as many cells as possible.
- Level
Medium6 of 10
- Topics
- Backtracking, Matrix, Implementation
- Solved
- No attempts yet
Problem
Fereydun, the legendary Persian hero whose prophecy was to overcome Zahhak, believes that he needs a powerful mind together with a powerful body. He has just learned a new brain teaser called Sudoku from a Japanese trader. Sudoku is played on a board that is a grid of cells. The whole grid is also partitioned into sub-grids, each of size . Each cell can be empty or contain an integer from to (inclusive). A Sudoku board is valid if it meets the following conditions: 1. All numbers in each row are distinct. 2. All numbers in each column are distinct. 3. All numbers in each sub-grid are distinct. The following figure shows two valid Sudoku boards with no empty cells:
Fereydun has a valid board with some empty cells and has asked for your help. Fill as many empty cells as you can while keeping the board valid. It is guaranteed that at least one way exists to fill all the empty cells while keeping the board valid. This is an output-only problem, so no source code submission is required. There are test cases. For each test case, you are given an input file containing a table as the initial state of a Sudoku board. Submit an output file containing a table as the final state of that board after filling some of its empty cells. All and values (for ) are integers from to (inclusive), where marks an empty cell.
Input
The first line contains . Line (for ) contains , , ..., , separated by spaces.
Output
Line (for ) must contain , , ..., , separated by spaces.
Constraints
. . The board is valid, and all of its empty cells can be filled while keeping it valid.