AcWing 756. 蛇形矩阵
原题链接
简单
作者:
时过境迁
,
2020-10-15 18:17:15
,
所有人可见
,
阅读 313
#include <cstdio>
#include <iostream>
using namespace std;
int arr[105][105];
int n, m;
int x = 1, y = 0;
int k = 1;
int main()
{
scanf("%d%d", &n, &m);
while(k <= n*m)
{
while(y < m && !arr[x][y+1]) arr[x][++y] = k++;
while(x < n && !arr[x+1][y]) arr[++x][y] = k++;
while(y > 1 && !arr[x][y-1]) arr[x][--y] = k++;
while(x > 1 && !arr[x-1][y]) arr[--x][y] = k++;
}
for(int i = 1; i <= n; ++i)
{
for(int j = 1; j <= m; ++j)
{
printf("%d ", arr[i][j]);
}
cout<<endl;
}
return 0;
}