实现
#include <cstdio>
#define MAXN 0x1ff
int directions[4][2] = {
{0,1}, // right
{1,-1}, // left and down
{1,0}, // down
{-1,1} // right and up
};
int size;
void getDirection(int* dir,int x, int y) {
if (*dir == 0) {
if (x == 0) {
*dir = 1;
} else {
*dir = 3;
}
} else if (*dir == 1) {
if (x == size - 1) {
*dir = 0;
} else if (y == 0) {
*dir = 2;
} else {
*dir = 1;
}
} else if (*dir == 2) {
if (y == 0) {
*dir = 3;
} else {
*dir = 1;
}
} else {
if (y == size - 1) {
*dir = 2;
} else if (x == 0) {
*dir = 0;
} else {
*dir == 3;
}
}
}
int map[MAXN][MAXN];
int main() {
scanf("%d",&size);
for (int i = 0;i < size;++i) {
for (int j = 0;j < size;++j) {
scanf("%d",&map[i][j]);
}
}
int x = 0, y = 0,dir = 0;
for (int i = 0; i < size;++i) {
for (int j = 0;j < size;++j) {
printf("%d ",map[x][y]);
x += directions[dir][0];
y += directions[dir][1];
getDirection(&dir,x,y);
}
}
}