从(1,1)z字形扫描到(N,N),
容易看出第i组需要扫描i次,总共2N组
奇数组从(i, 1)扫描到(1, i),
偶数组从(1, i)扫描到(i, 1)。
设操作数为j <=i, 当前组数y
可得 t = i & 1 ? q[y][j] : q[j][y]
#include <bits/stdc++.h>
using namespace std;
const int N = 510;
int q[2 * N][2 * N], res[N*N], n, k;
int main()
{
cin >> n;
for (int i = 1; i <= n; i ++)
for (int j = 1; j <= n; j ++)
cin >> q[i][j];
res[k ++] = q[1][1];
for (int i = 2; i <= 2 * n; i ++)
for (int y = i, j = 1; j <= i; j ++, y --)
{
int t = i & 1 ? q[y][j] : q[j][y];
if (!t) continue;
res[k ++] = t;
}
for (int i = 0; i < n * n; i ++)
if (i) cout << ' ' << res[i];
else cout << res[0];
return 0;
}