AcWing 756. 蛇形矩阵
原题链接
简单
作者:
zzoce
,
2021-02-20 23:26:29
,
所有人可见
,
阅读 312
#include <iostream>
#include <cmath>
using namespace std;
int n,m;
int map[120][120];
void f (int x,int y,int cnt)
{
if (map[x][y]!=0)
{
return ;
}
for (int i=y;i<=m-x+1;i++)
{
if (map[x][i]==0)
{
map[x][i]=cnt;
cnt++;
}
}
for (int i=x+1;i<=n-x+1;i++)
{
if (map[i][m-x+1]==0)
{
map[i][m-x+1]=cnt;
cnt++;
}
}
for (int i=m-x;i>=y;i--)
{
if (map[n-x+1][i]==0)
{
map[n-x+1][i]=cnt;
cnt++;
}
}
for (int i=n-x;i>x;i--)
{
if (map[i][x]==0)
{
map[i][x]=cnt;
cnt++;
}
}
f (x+1,y+1,cnt);
}
int main ()
{
cin>>n>>m;
f (1,1,1);
for (int i=1;i<=n;i++)
{
cout<<map[i][1];
for (int j=2;j<=m;j++)
{
cout<<" "<<map[i][j];
}
cout<<endl;
}
return 0;
}