代码
#include <bits/stdc++.h>
using namespace std;
int f[500][500];
int d[5][3]={{0,1},{1,0},{0,-1},{-1,0}};
int main()
{
int n,m;
cin>>n>>m;
int x=1,y=1,ori=0;
for(int i=1;i<=n*m;i++)
{
int t1=x+d[ori][0],t2=y+d[ori][1];
if(t1>n||t2>m||t1<=0||t2<=0||f[t1][t2]) ori=(ori+1)%4;
f[x][y]=i;
x+=d[ori][0];
y+=d[ori][1];
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
printf("%d ",f[i][j]);
puts("");
}
return 0;
}