#P1274. 魔术数字游戏

魔术数字游戏

Description

There are many variations of number-filling grid games. In the 4×44 \times 4 grid below, we are to fill the sixteen cells with the numbers 11 through 1616 (cells Ai,jA_{i,j}, where i=14i = 1 \cdots 4, j=14j = 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 3434:

A1,1A_{1,1} A1,2A_{1,2} A1,3A_{1,3} A1,4A_{1,4}
A2,1A_{2,1} A2,2A_{2,2} A2,3A_{2,3} A2,4A_{2,4}
A3,1A_{3,1} A3,2A_{3,2} A3,3A_{3,3} A3,4A_{3,4}
A4,1A_{4,1} A4,2A_{4,2} A4,3A_{4,3} A4,4A_{4,4}
  • The four corner cells: A1,1+A1,4+A4,1+A4,4=34A_{1,1}+A_{1,4}+A_{4,1}+A_{4,4}=34.
  • Each 2×22 \times 2 block at a corner, for example the top-left: A1,1+A1,2+A2,1+A2,2=34A_{1,1}+A_{1,2}+A_{2,1}+A_{2,2}=34.
  • The central 2×22 \times 2 block: A2,2+A2,3+A3,2+A3,3=34A_{2,2}+A_{2,3}+A_{3,2}+A_{3,3}=34.
  • Each row: Ai,1+Ai,2+Ai,3+Ai,4=34A_{i,1}+A_{i,2}+A_{i,3}+A_{i,4}=34, where i=14i = 1 \cdots 4.
  • Each column: A1,j+A2,j+A3,j+A4,j=34A_{1,j}+A_{2,j}+A_{3,j}+A_{4,j}=34, where j=14j = 1 \cdots 4.
  • The two diagonals, for example the main diagonal from top-left to bottom-right: A1,1+A2,2+A3,3+A4,4=34A_{1,1}+A_{2,2}+A_{3,3}+A_{4,4}=34.
  • From top-right to bottom-left: A1,4+A2,3+A3,2+A4,1=34A_{1,4}+A_{2,3}+A_{3,2}+A_{4,1}=34.

In particular, we will fix the number 11 in a specified cell.

Input Format

The input contains a single line with two positive integers ii and jj, indicating that the cell in row ii and column jj contains the number 11. Fill the remaining fifteen cells with the numbers 22 through 1616 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.

1 1

1 4 13 16 
14 15 2 3 
8 5 12 9 
11 10 7 6 

1 4 13 16 
14 15 2 3 
12 9 8 5 
7 6 11 10 

……剩余214组合法解省略

Hint

For the sample, there are 216216 valid fillings.

Constraints: For all test points, it is guaranteed that 1i,j41 \leq i, j \leq 4.

Translated by ChatGPT 5