XDU 1055
#include<stdio.h> #include<cstring> int main() { //freopen("orz.txt","w",stdout); int i,j,n,m,x,y; while(scanf("%d%d",&n,&m)!=EOF) { int c[n][m]; int d[n*m]; memset(c,0,sizeof(0)); for(i=0;i<n;i++){ for(j=0;j<m;j++) scanf("%d",&c[i][j]); } d[0]=c[x=0][y=0]; int vis=1,k=0; while(vis<n*m) { if(x+1<n){ d[++k]=c[++x][y]; vis++; } else{ d[++k]=c[x][++y]; vis++; } while(x>0&&y<m-1) { d[++k]=c[--x][++y]; vis++; } if(y+1<m){ d[++k]=c[x][++y]; vis++; } else{ d[++k]=c[++x][y]; vis++; } while(y>0&&x<n-1) { d[++k]=c[++x][--y]; vis++; } } for(int i=0;i<n*m-1;i++) printf("%d ",d[i]); printf("%d\n",d[n*m-1]); } return 0; }