VisaOA #7
Restore Missing Squares in Matrix
估计简单可在线判题
考点(AI 标注)模拟/实现原地数组
A square matrix with side 4n is divided into n² 4×4 sub-blocks. Each sub-block contains every integer in 1..16 except one, and the missing position is marked -1. First subtract the sum of the numbers present in each sub-block from 136 to get that block's missing value; then sort all missing values in ascending order and fill them into all -1 positions of the original matrix in turn. Positions are ordered row-major (row first, then column) over the whole matrix, not by sub-block order. This site limits 1≤n≤100.
输入格式
The first line contains an integer n (1..100), meaning each side has n 4×4 sub-blocks. Then 4n lines follow, each with 4n integers. Each sub-block has exactly one -1, and its other values are 15 distinct integers from 1..16 excluding one value.
输出格式
Output the restored matrix of 4n rows, each with 4n integers.
样例 1
输入:
1
1 2 3 4
5 -1 6 7
8 10 11 12
13 14 15 16
输出:
1 2 3 4
5 9 6 7
8 10 11 12
13 14 15 16
样例 2
输入:
2
1 2 3 4 1 3 4 5
5 6 7 8 -1 6 7 8
9 10 11 12 9 10 11 12
-1 13 14 16 13 14 15 16
-1 1 2 3 1 2 3 5
4 5 6 7 6 7 8 9
8 9 10 11 10 11 12 13
12 14 15 16 14 15 16 -1
输出:
1 2 3 4 1 3 4 5
5 6 7 8 2 6 7 8
9 10 11 12 9 10 11 12
4 13 14 16 13 14 15 16
13 1 2 3 1 2 3 5
4 5 6 7 6 7 8 9
8 9 10 11 10 11 12 13
12 14 15 16 14 15 16 15
样例 3
输入:
2
-1 1 2 3 1 2 3 -1
4 5 6 8 4 5 6 8
9 10 11 12 9 10 11 12
13 14 15 16 13 14 15 16
1 2 4 5 1 2 4 5
6 7 8 9 6 7 8 9
10 11 12 13 10 11 12 13
-1 14 15 16 14 15 16 -1
输出:
3 1 2 3 1 2 3 3
4 5 6 8 4 5 6 8
9 10 11 12 9 10 11 12
13 14 15 16 13 14 15 16
1 2 4 5 1 2 4 5
6 7 8 9 6 7 8 9
10 11 12 13 10 11 12 13
7 14 15 16 14 15 16 7
样例说明
In example 2, the missing positions seen in sub-block order differ from their row-major positions in the whole matrix; so you must first sort the missing values independently, then fill them back in the global row-major order of all -1 positions.