AcWing 756. 蛇形矩阵
原题链接
简单
作者:
auzers
,
2025-01-08 10:10:15
,
所有人可见
,
阅读 2
#include <cstdio>
#include <iostream>
using namespace std;
const int N = 100;
int a[N][N];
int main()
{
int n,m;
cin >> n >> m;
int i,tmp;
int dx = 0,dy = 1;
int x = 0,y = 0,tx = 0,ty = 0;
for(i = 1;i <= n * m;i ++)
{
a[x][y] = i;
tx = x + dx;
ty = y + dy;
if(tx >= n || ty >= m || ty < 0 || a[tx][ty] != 0)//碰到边界就换方向
{
tmp = dx;
dx = dy;
dy = -tmp;
}
x = x + dx;
y = y + dy;
}
for (x = 0;x < n;x++)
{
for (y = 0;y < m;y++)
{
cout << a[x][y] << ' ';
}
cout << endl;
}
return 0;
}