P1274 Magic Number Game
Description
There are many variations of number-filling grid games. In the $4 \times 4$ grid below, we are to fill the sixteen cells with the numbers $1$ through $16$ (cells $A_{i,j}$, where $i = 1 \cdots 4$, $j = 1 \cdots 4$). To make the game more challenging, we require that the four numbers specified in each of the following items must add up to $34$:
| $A_{1,1}$ | $A_{1,2}$ | $A_{1,3}$ | $A_{1,4}$|
| :----------- | :----------- | :----------- | :----------- |
| $A_{2,1}$ | $A_{2,2}$ | $A_{2,3}$ | $A_{2,4}$ |
| $A_{3,1}$ | $A_{3,2}$ | $A_{3,3}$ | $A_{3,4}$ |
| $A_{4,1}$ | $A_{4,2}$ | $A_{4,3}$ | $A_{4,4}$ |
- The four corner cells: $A_{1,1}+A_{1,4}+A_{4,1}+A_{4,4}=34$.
- Each $2 \times 2$ block at a corner, for example the top-left: $A_{1,1}+A_{1,2}+A_{2,1}+A_{2,2}=34$.
- The central $2 \times 2$ block: $A_{2,2}+A_{2,3}+A_{3,2}+A_{3,3}=34$.
- Each row: $A_{i,1}+A_{i,2}+A_{i,3}+A_{i,4}=34$, where $i = 1 \cdots 4$.
- Each column: $A_{1,j}+A_{2,j}+A_{3,j}+A_{4,j}=34$, where $j = 1 \cdots 4$.
- The two diagonals, for example the main diagonal from top-left to bottom-right: $A_{1,1}+A_{2,2}+A_{3,3}+A_{4,4}=34$.
- From top-right to bottom-left: $A_{1,4}+A_{2,3}+A_{3,2}+A_{4,1}=34$.
In particular, we will fix the number $1$ in a specified cell.
Input Format
The input contains a single line with two positive integers $i$ and $j$, indicating that the cell in row $i$ and column $j$ contains the number $1$. Fill the remaining fifteen cells with the numbers $2$ through $16$ according to the conditions above.
Output Format
Output all valid solutions in increasing order, separated by a single empty line.
For each valid solution, output four lines, each containing four numbers separated by a single space.
The ordering of valid solutions is determined as follows: compare numbers starting from the first row; within each row, compare from the leftmost number. The solution with the smaller number at the first differing position must be printed first.
Explanation/Hint
For the sample, there are $216$ valid fillings.
Constraints: For all test points, it is guaranteed that $1 \leq i, j \leq 4$.
Translated by ChatGPT 5