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

    ID: 13625 远端评测题 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!

题目大意

【题目描述】

宝宝女巫被困在一个 nnnn 列的迷宫中,其中第 ii 行第 jj 列的单元格高度为 hi,jh_{i,j}。要走出迷宫,宝宝必须找到一条路径,该路径穿过每个单元格恰好一次。每次她只能移动到与当前单元格共享边的相邻单元格。但是众所周知,宝宝非常懒,所以每当她爬升(即从高度较低的单元格移动到高度较高的单元格)时,她的幸福值会减少。作为她的帮手,你的任务是找到一条有效的路径,使得沿着路径移动时,宝宝爬升的次数不多于她下降的次数。

更正式地说,你需要找到一个序列 (x1,y1),(x2,y2),,(xn2,yn2)(x_1, y_1), (x_2, y_2), \cdots, (x_{n^2}, y_{n^2}),使得:

  • 对于所有的 1in21 \le i \le n^21xi,yin 1 \le x_i, y_i \le n
  • 对于所有的 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)
  • 对于所有的 2in22 \le i \le n^2xixi1+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}]}$,其中 [P][P]PP 为真时等于 11,当为假时等于 00

此外,你发现所有单元格的高度都是 n2n^2 的排列,所以你只需要输出有效路径中每个单元格的高度。

【输入格式】

有多个测试用例。输入的第一行包含一个整数 TT1T1001 \le T \le 100),表示测试用例的数量。对于每个测试用例:

第一行包含一个整数 nn2n642 \le n \le 64),表示迷宫的大小。

接下来的 nn 行,第 ii 行包含 nn 个整数 hi,1,hi,2,,hi,nh_{i, 1}, h_{i, 2}, \cdots, h_{i,n}1hi,jn21 \le h_{i, j} \le n^2),其中 hi,jh_{i,j} 表示第 ii 行第 jj 列单元格的高度。保证输入中的所有整数构成 n2n^2 的排列。

【输出格式】

对于每个测试用例,输出一行,包含 n2n^2 个由空格分隔的整数,表示有效路径中每个单元格的高度。如果有多个有效答案,你可以输出其中任何一个。很容易证明答案总是存在。

请不要在每行的末尾输出多余的空格,否则你的答案可能会被认为不正确!

翻译来自于:ChatGPT

1
2
4 3
2 1
4 3 1 2