AcWing 756. 蛇形矩阵
原题链接
简单
作者:
深街酒徒
,
2024-11-22 17:02:42
,
所有人可见
,
阅读 1
#include <iostream>
using namespace std;
int res[105][105];
int main ()
{
int n, m;
cin >> n >> m;
int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
for(int x = 0, y = 0, k = 1, d = 0; k <= n * m; k++)
{
res[x][y] = k;
int a = x + dx[d], b = y + dy[d];
if(a < 0 || a >= n || b < 0 || b >= m || res[a][b])
{
d = (d + 1) % 4;
a = x + dx[d], b = y + dy[d];
}
x = a, y = b;
}
for(int i = 0; i < n; i++)
{
for(int j = 0; j < n; j++)
cout << res[i][j] << " ";
cout << endl;
}
return 0;
}