Tasks:
 
Task DescriptionDiscussion (0)
Task :: z-sudoku2
Since last Saturday, when he earned the basic rules of Sudoku, Vanja has been practicing a lot, and is now ready to actually solve sudoku puzzles.

Sudoku is a game in which we have to fill out 9 different characters into a 9x9 mesh, with the following rules.:
• Every character has to be used exactly 9 times.
• There can not be 2 same characters in a single row.
• There can not be 2 same characters in a single column.
• There can not be 2 same characters in any of the 3x3 sub-squares (as shown on the figure below).

+-----+-----+-----+
|6 9 2|7 1 5|4 8 3|
|1 5 3|4 2 8|7 9 6|
|8 4 7|9 3 6|2 5 1|
+-----+-----+-----+
|2 8 5|3 6 1|9 7 4|
|3 7 4|5 8 9|6 1 2|
|9 6 1|2 7 4|5 3 8|
+-----+-----+-----+
|5 3 9|8 4 2|1 6 7|
|4 1 8|6 9 7|3 2 5|
|7 2 6|1 5 3|8 4 9|
+-----+-----+-----+

Vanja has developed his own technique for solving sudoku puzzles, that is pretty much different then Little Z's technique. When a puzzle has no solutions, then they both find that there is no solution. When the puzzle has an unique solution, then they both find the same (unique solution). But when the puzzle doesn't have an unique solution, then Little Z. find the lexicographically smallest one, whereas Vanja find the lexicographically largest one. Now your job is to, for given puzzles predict both Little Z's and Vanja's solution of the puzzle.

INPUT:
The first 9 lines of the standard input will contain 9 characters, representing the puzzle. The characters will be 1 to 9 or . (dot) The dot represents an empty field.

OUTPUT:
To the standard output you should write the lexicographically smallest solution of the given puzzle, (in the format 9x9, line in the input), then one empty line, and finally the lexicographically largest solution (the same 9x9 format).
If the solution is unique, then you should only write that one (uniqe) solution - again 9x9 format.
If there is no solution then write "IMPOSSIBLE".

Input 1:
..9.5..38
78..46..2
2..8..417
57..31..4
..45....9
92..74..5
1..38.1.6
..2..9..7
..576..41

Output 1:
IMPOSSIBLE

Explanation: The lower-right 3x3 sub-square has the number 1 occurring twice.

Input 2:
....1....
.9..5.4.3
7.846.1.5
926.85...
1.......2
...62.591
8.9.723.6
6.2.9..4.
....4....

Output 2:
564213978
291758463
738469125
926185734
175934682
483627591
849572316
652391847
317846259

Explanation: The solution is unique, so the output contains only the unique solution.

Input 3:
.........
.........
.........
.........
.........
.........
.........
.........
.........

Output 3:
123456789
456789123
789123456
214365897
365897214
897214365
531642978
642978531
978531642

987654321
654321987
321987654
896745213
745213896
213896745
579468132
468132579
132579468

Explanation: This puzzle has multiple solutions, so we output the lexicographically smallest, and the largest one.

Submit Solution
:
:
Available Languages
Task info
Name:z-sudoku2
Time:2 sec.
Memory:16 MB
#Tests:10
AddedBy: oduleodule
Source:Dusko Obradovic
Task Ratings
Difficulty:

4.7 (15 votes)
Quality:

4.4 (12 votes)
Acceptance Rate
Recent Submissions
Fastest Solutions
UserTime
halil 0.09 s.
dejandenib 0.435 s.
marko1597 1.095 s.
A.Armin 1.305 s.
gates 1.31 s.
cedricl 2.55 s.
oduleodule 3.315 s.
picsel 5.81 s.
Solved By