纯粹模拟。
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std ;
int main()
{
int n,x,y;
int a[55][55];
while(scanf("%d",&n)&&n){
for(int i=0;i<n;i++) for(int j=0;j<n;j++){
scanf("%d",&a[i][j]);
}
x=0,y=0;
printf("%d",a[x][y]);
while(1){
if(x==n-1&&y==n-1) break;
if(x==0&&y<n-1){
printf(" %d",a[x][++y]);
while(y>0){
printf(" %d",a[++x][--y]);
}
}
if(y==0&&x<n-1){
printf(" %d",a[++x][y]);
while(x>0) printf(" %d",a[--x][++y]);
}
if(x==n-1&&y<n-1){
printf( " %d",a[x][++y]);
while(y<n-1) printf(" %d",a[--x][++y]);
}
if(y==n-1&&x<n-1){
printf(" %d",a[++x][y]);
while(x<n-1) printf(" %d",a[++x][--y]);
}
}
puts("");
}
return 0 ;
}