分奇偶来写
#include<iostream>
#include<cstdio>
using namespace std;
const int N = 1010;
int arr[N][N];
int main()
{
int n;
cin >> n;
int l = 1;
int x = 1, y = 1;
if (n % 2 == 1) {
while (l <= n * n)
{
if (y == n && n % 2 == 0 && x % 2 == 1) { arr[x][y] = l++; x++; continue; }
if (y == n && n % 2 == 0 && x % 2 == 0) { arr[x][y] = l++; x++, y--; continue; }
if (y == n && n % 2 == 1 && x % 2 == 1) { arr[x][y] = l++; y--, x++; continue; }
if (y == n && n % 2 == 1 && x % 2 == 0) { arr[x][y] = l++; x++; continue; }
if (x == n && n % 2 == 1 && y % 2 == 1) { arr[x][y] = l++; y++; continue; }
if (x == n && n % 2 == 1 && y % 2 == 0) { arr[x][y] = l++; x--, y++; continue; }
if (x % 2 == 1 && y == 1) { arr[x][y] = l++; x++; continue; }
if (x % 2 == 0 && y == 1) { arr[x][y] = l++; y++, x--; continue; }
if (y % 2 == 0 && x == 1) { arr[x][y] = l++; y++; continue; }
if (y % 2 == 1 && x == 1) { arr[x][y] = l++; y--, x++; continue; }
if ((x + y) % 2 == 1) { arr[x][y] = l++; x--, y++; continue; }
if ((x + y) % 2 == 0) { arr[x][y] = l++; x++, y--; continue; }
}
}
else {
while (l <= n * n)
{
if (y == n && n % 2 == 0 && x % 2 == 1) { arr[x][y] = l++; x++; continue; }
if (y == n && n % 2 == 0 && x % 2 == 0) { arr[x][y] = l++; x++, y--; continue; }
if (y == n && n % 2 == 1 && x % 2 == 1) { arr[x][y] = l++; y--, x++; continue; }
if (y == n && n % 2 == 1 && x % 2 == 0) { arr[x][y] = l++; x++; continue; }
if (x == n && y % 2 == 1) { arr[x][y] = l++; x--, y++; continue; }
if (x == n && y % 2 == 0) { arr[x][y] = l++; y++; continue; }
if (x % 2 == 1 && y == 1) { arr[x][y] = l++; x++; continue; }
if (x % 2 == 0 && y == 1) { arr[x][y] = l++; y++, x--; continue; }
if (y % 2 == 0 && x == 1) { arr[x][y] = l++; y++; continue; }
if (y % 2 == 1 && x == 1) { arr[x][y] = l++; y--, x++; continue; }
if ((x + y) % 2 == 1) { arr[x][y] = l++; x--, y++; continue; }
if ((x + y) % 2 == 0) { arr[x][y] = l++; x++, y--; continue; }
}
}
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
printf("%d ", arr[j][i]);
puts("");
}
return 0;
}