Run ID:158311
提交时间:2026-08-20 18:05:15
#include <iostream> #include <cstring> using namespace std; int main(){ int N; cin>>N; int a[N][N]; memset(a,0,sizeof(a)); int d=1; int y=0; int x=N-1; int v=1; int nn=N*N; while(v<=nn){ a[y][x]=v; bool zw; if(d==1){ zw=false; if(y==n-1) zw=true; else if(a[y+1][x]>0) zw=true; if(zw){ d=2; x--; } else{ y++; } } else if(d==2){ zw=false; if(x==0) zw=true; else if(a[y][x-1]>0) zw=true; if(zw){ d=3; y--; } else{ x--; } } else if(d==3){ zw=false; if(y==0) zw=true; else if(a[y-1][x]>0) zw=true; if(zw){ d=4; x++; } else{ y--; } } else{ zw=false; if(x==N-1) zw=true; else if(a[y][x+1]>0) zw=true; if(zw){ d=1; y++; } else{ x++; } } v++; } for(int y=0;y<N;y++){ for(int x=0;x<N;x++){ if(x>0) cout<<" "; cout<<a[y][x]; } cout<<endl; } return 0; }