#K57932. Complete the Magic Square

    ID: 30530 Type: Default 1000ms 256MiB

Complete the Magic Square

Complete the Magic Square

You are given a partially filled 3×3 grid. Some cells contain numbers and the rest are filled with 0 (zero) indicating an empty cell. Your task is to fill in the missing numbers with distinct integers from 1 to 9 (each exactly once) so that the resulting grid becomes a magic square. A magic square is a square in which the sums of every row, every column, and the two main diagonals are equal. In this problem, the magic constant for a 3×3 magic square is \(15\), i.e., every row, column, and diagonal must sum to \(15\).

If there is a valid way to complete the grid, output the completed magic square; otherwise, output -1.

Note: The input is read from standard input and the output must be printed to standard output.

inputFormat

The input consists of three lines. Each line contains three space-separated integers representing a row of the 3×3 grid. A 0 represents an empty cell.

outputFormat

If the grid can be completed to form a magic square, output three lines each containing three space-separated integers of the completed grid. If no valid magic square can be formed, output -1.## sample

8 1 6
3 0 0
0 0 0
8 1 6

3 5 7 4 9 2

</p>