Logo
(追記) (追記ここまで)

31688번 - So I’ll Max Out My Constructive Algorithm Skills 스페셜 저지다국어

시간 제한메모리 제한제출정답맞힌 사람정답 비율
1 초 1024 MB69413759.677%

문제

BaoBao the Witch is stuck in a maze with $n$ rows and $n$ columns, where the height of the cell in the $i$-th row and the $j$-th column is $h_{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 $(x_1, y_1),(x_2, y_2), \cdots ,(x_{n^2}, y_{n^2})$ such that:

  • For all 1ドル ≤ i ≤ n^2,ドル 1ドル ≤ x_i , y_i ≤ n$;
  • For all 1ドル ≤ i, j ≤ n^2,ドル $i \ne j,ドル $(x_i , y_i) \ne (x_j , y_j )$;
  • For all 2ドル ≤ i ≤ n^2,ドル $|x_i - x_{i-1}| + |y_i - y_{i-1}| = 1$;
  • $\displaystyle\sum_{i=2}^{n^2}{[h_{x_{i-1},y_{i-1}} < h_{x_i, y_i}]} \le \displaystyle\sum_{i=2}^{n^2}{[h_{x_{i-1},y_{i-1}} > h_{x_i, y_i}]},ドル where $[P]$ equals 1ドル$ when $P$ is true, and equals 0ドル$ when it is false.

Additionally, you discover that the heights in all cells are a permutation of $n^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 $T$ (1ドル ≤ T ≤ 100$) indicating the number of test cases. For each test case:

The first line contains an integer $n$ (2ドル ≤ n ≤ 64$) indicating the size of the maze.

For the following $n$ lines, the $i$-th line contains $n$ integers $h_{i,1}, h_{i,2}, \cdots , h_{i,n}$ (1ドル ≤ h_{i,j} ≤ n^2$) where $h_{i,j}$ indicates the height of the cell in the $i$-th row and the $j$-th column. It’s guaranteed that all integers in the input make up a permutation of $n^2$.

출력

For each test case output one line containing $n^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.

제한

예제 입력 1

1
2
4 3
2 1

예제 출력 1

4 3 1 2

힌트

출처

Contest > Waterloo's local Programming Contests > Waterloo Winter 2023 Local A번

(追記) (追記ここまで)

출처

대학교 대회

  • 사업자 등록 번호: 541-88-00682
  • 대표자명: 최백준
  • 주소: 서울시 서초구 서초대로74길 29 서초파라곤 412호
  • 전화번호: 02-521-0487 (이메일로 연락 주세요)
  • 이메일: contacts@startlink.io
  • 통신판매신고번호: 제 2017-서울서초-2193 호

AltStyle によって変換されたページ (->オリジナル) /