#K10111. Complete the Magic Square
Complete the Magic Square
Complete the Magic Square
You are given a partially filled \( n \times n \) grid. Your task is to complete this grid to form a magic square, where the sum of the numbers in each row, each column, and both main diagonals are equal. For a magic square of size \( n \), this common sum (the magic constant) is given by \( M = \frac{n(n^2+1)}{2} \).
The grid may have some pre-filled numbers, and zero represents an empty cell. You must fill in the empty cells with numbers from \( 1 \) to \( n^2 \) (each exactly once) such that the final grid forms a magic square. If it is possible to complete the grid, output POSSIBLE
followed by the completed grid. Otherwise, output IMPOSSIBLE
.
It is guaranteed that \( n = 3 \) for all test cases.
inputFormat
The first line contains a single integer \( n \) (which is 3 in all test cases). Each of the next \( n \) lines contains \( n \) integers representing the grid. Empty cells are denoted by 0.
Example:
3 0 0 0 0 0 0 0 0 0
outputFormat
If it is possible to complete the grid to form a magic square, print POSSIBLE
on the first line, and then print the \( n \) lines of the completed magic square (each line contains \( n \) space-separated integers). Otherwise, print IMPOSSIBLE
.
3
0 0 0
0 0 0
0 0 0
POSSIBLE
2 7 6
9 5 1
4 3 8
</p>