Run ID:148584

提交时间:2026-02-26 15:51:04

#include<bits/stdc++.h> using namespace std; int a[20][20]; int main() { int n,x,y,tot; cin>>n; tot=a[x=1][y=1]=1; while(tot<=n*n){ while(x+1<=n&&!a[x+1][y]) a[++x][y]=++tot; a[x][++y]=++tot; while(x-1>=1&&!a[x-1][y]) a[--x][y]=++tot; a[x][++y]=++tot; } for(int i=1;i<=n;i++){ for(int j=1;j<=n;j++){ cout<<a[i][j]<<" "; } cout<<endl; } return 0; }