@[Qiuxi](/user/976976)
```cpp
#include<bits/stdc++.h>
using namespace std;
int a[1002][1002];
int main()
{
int n,x=1,y,tot=0;
cin>>n;
y=0;
memset(a,0,sizeof(a));
tot=a[x][y]=1;
while(tot<=n*n){
while(y<n&&!a[x][y+1])a[x][++y]=tot++;
while(x<n&&!a[x+1][y])a[++x][y]=tot++;
while(y>1&&!a[x][y-1])a[x][--y]=tot++;
while(x>1&&!a[x-1][y])a[--x][y]=tot++;
}
for(x=1;x<=n;++x)
{
for (y=1;y<=n;++y) printf("%3d ",a[x][y]);
cout<<endl;
}
return 0;
}
```
by huangboning @ 2023-06-03 17:22:52
```
#include<bits/stdc++.h>
using namespace std;
int n,a[15][15],x=1,y=0,cnt=0;
int main(){
cin>>n;
while(cnt<n*n){
while(a[x][y+1]==0&&y+1<=n){
a[x][++y]=++cnt;
}
while(a[x+1][y]==0&&x+1<=n){
a[++x][y]=++cnt;
}
while(a[x][y-1]==0&&y-1>=1){
a[x][--y]=++cnt;
}
while(a[x-1][y]==0&&x-1>=1){
a[--x][y]=++cnt;
}
}
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++)
cout<<setw(3)<<a[i][j];
cout<<endl;
}
return 0;
}
```
@[Qiuxi](/user/976976)
by gongqiheng @ 2023-08-12 09:18:11