#P1277. Solve the 4×4 Matrix Puzzle
Solve the 4×4 Matrix Puzzle
Solve the 4×4 Matrix Puzzle
You are given an unknown 4×4 matrix \(M\) where every element is a positive integer. The sums of the 4 rows, the 4 columns, and the two diagonals (the main diagonal and the anti-diagonal) are provided. In addition, the values at 4 specific positions in the matrix are given (the positions are provided in the input).
Your task is to determine the values of the other 12 positions in the matrix such that the row sums, column sums, and diagonal sums match the given values.
Note: All formulas are given in \(\LaTeX\) format. For example, if \(R_i\) denotes the sum of the \(i\)th row then \(R_i = \sum_{j=1}^{4} M_{ij}\). Similarly, if \(C_j\) denotes the sum of the \(j\)th column then \(C_j = \sum_{i=1}^{4} M_{ij}\). The sums of the main diagonal and the anti-diagonal are given by \(D_1 = M_{11}+M_{22}+M_{33}+M_{44}\) and \(D_2 = M_{14}+M_{23}+M_{32}+M_{41}\) respectively.
inputFormat
The input consists of the following lines:
- First line: 4 space-separated integers representing the sums of the 4 rows \(R_1, R_2, R_3, R_4\).
- Second line: 4 space-separated integers representing the sums of the 4 columns \(C_1, C_2, C_3, C_4\).
- Third line: 2 space-separated integers representing the sums of the two diagonals \(D_1\) (main diagonal) and \(D_2\) (anti-diagonal).
- Next 4 lines: Each line contains 3 space-separated integers \(r\), \(c\) and \(v\) representing that the element at row \(r\) and column \(c\) (1-indexed) of the matrix has the value \(v\).
outputFormat
Output the complete 4×4 matrix in 4 lines. Each line should contain 4 integers separated by a space representing the row of the matrix.
sample
10 26 42 58
28 32 36 40
34 34
1 2 2
2 3 7
4 1 13
3 4 12
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
</p>