#C5065. 5x5 Magic Square Completion Puzzle
5x5 Magic Square Completion Puzzle
5x5 Magic Square Completion Puzzle
You are given a partially filled 5x5 grid representing a magic square puzzle. Some cells contain a number between 1 and 25 and the empty cells are represented by 0
. Your task is to fill in the missing cells with the remaining unique numbers (each number from 1 to 25 must appear exactly once) so that every row, every column, and both main diagonals sum to \(65\). Recall that the magic constant for a 5x5 magic square is given by \(65 = \frac{5(5^2+1)}{2}\).
The input grid is guaranteed to have a unique solution.
inputFormat
The input consists of 5 lines. Each line contains 5 space-separated integers. The number 0
represents an empty cell that must be filled.
outputFormat
Output the completed magic square in 5 lines. Each line should contain 5 space-separated integers.
## sample17 24 1 8 15
23 5 7 14 0
4 6 13 20 22
10 12 19 0 0
11 18 0 0 0
17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9
</p>