#P9656. [ICPC2021 Macao R] So I'll Max Out My Constructive Algorithm Skills

    ID: 9018 远端评测题 1000ms 256MiB 尝试: 1 已通过: 1 难度: 2 上传者: 标签>模拟2021Special JudgeO2优化ICPCAd-hoc澳门

[ICPC2021 Macao R] So I'll Max Out My Constructive Algorithm Skills

题目描述

BaoBao the Witch is stuck in a maze with nn rows and nn columns, where the height of the cell in the ii-th row and the jj-th column is hi,jh_{i,j}. To get out of the maze, BaoBao has to find a path which passes through each cell exactly once. Each time she can only move into the neighboring cell sharing a same edge with the current one. But as we know, BaoBao is super lazy, so every time when she climbs up (that is to say, moving from a cell with a smaller height to another with a larger height) her happiness value will decrease. As her helping hand, your task is to find a valid path so that when moving along the path, the number of times BaoBao climbs up will not be more than the number of times she climbs down.

More formally, you need to find a sequence (x1,y1),(x2,y2),,(xn2,yn2)(x_1, y_1), (x_2, y_2), \cdots, (x_{n^2}, y_{n^2}) such that:

  • For all 1in21 \le i \le n^2, 1xi,yin 1 \le x_i, y_i \le n;
  • For all 1i,jn2,ij1 \le i, j \le n^2, i \neq j, (xi,yi)(xj,yj) (x_i, y_i) \neq (x_j, y_j);
  • For all 2in22 \le i \le n^2, xixi1+yiyi1=1|x_i - x_{i-1}| + |y_i - y_{i-1}| = 1;
  • $\sum\limits_{i=2}^{n^2}{[h_{x_{i-1}, y_{i-1}} < h_{x_i, y_i}]} \le \sum\limits_{i=2}^{n^2}{[h_{x_{i-1}, y_{i-1}} > h_{x_i, y_i}]}$, where [P][P] equals 11 when PP is true, and equals 00 when it is false.

Additionally, you discover that the heights in all cells are a permutation of n2n^2, so you just need to output the height of each cell in a valid path.

输入格式

There are multiple test cases. The first line of the input contains an integer TT (1T1001 \le T \le 100) indicating the number of test cases. For each test case:

The first line contains an integer nn (2n642 \le n \le 64) indicating the size of the maze.

For the following nn lines, the ii-th line contains nn integers hi,1,hi,2,,hi,nh_{i, 1}, h_{i, 2}, \cdots, h_{i,n} (1hi,jn21 \le h_{i, j} \le n^2) where hi,jh_{i,j} indicates the height of the cell in the ii-th row and the jj-th column. It's guaranteed that all integers in the input make up a permutation of n2n^2.

输出格式

For each test case output one line containing n2n^2 separated by a space indicating the heights of each cell in a valid path. If there are multiple valid answers you can output any of them. It's easy to prove that an answer always exists.

Please, DO NOT output extra spaces at the end of each line, or your answer may be considered incorrect!

1
2
4 3
2 1
4 3 1 2