#P1277. 拼字游戏
拼字游戏
Description
There is an unknown grid , where each entry is a positive integer. You are given the sums of the rows, the sums of the columns, and the sums of both diagonals. In addition, the values of any positions in the grid are given; their positions are specified in the input.
Write a program to determine the positive integers in the remaining positions of the grid, such that the row sums, column sums, and diagonal sums match the values provided in the input.
You may assume that no row, column, or diagonal sum exceeds . You may also assume that a solution always exists for the given input.
Input Format
The input contains space-separated positive integers.
The first four numbers are the sums of the four rows, respectively; the next four numbers are the sums of the columns. The next number is the sum of the main diagonal elements, i.e., . The following number (the th number) is the sum of the anti-diagonal elements, i.e., . The remaining part contains numbers, grouped into triples , meaning .
Output Format
Output the numbers arranged as a grid; within each row, separate the four numbers by a single space. Note: For a given input, there may be more than one valid solution. Any one of them is acceptable.
130 120 172 140 157 93 144 168 66 195 0 1 15 1 3 49 2 2 16 3 0 33
22 15 28 65
49 1 21 49
53 76 16 27
33 1 79 27
Hint
- For of the testdata, no row, column, or diagonal sum exceeds .
- For of the testdata, no row, column, or diagonal sum exceeds .
- For of the testdata, no row, column, or diagonal sum exceeds .
- For of the testdata, no row, column, or diagonal sum exceeds .
Thanks to @Jomoo for the contribution.
Translated by ChatGPT 5
京公网安备 11011102002149号