AcWing 756. 蛇形矩阵
原题链接
简单
作者:
Jairus
,
2024-11-25 01:21:35
,
所有人可见
,
阅读 3
#include <iostream>
using namespace std;
int num[100][100] = {0};
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, d = 0, k = 1; k <= n * m; k++)
{
num[x][y] = k;
x = x + dx[d];
y = y + dy[d];
if (x < 0 || y < 0 || x >= n || y >= m || num[x][y])
{
d = (d + 1) % 4;
x = x + dx[d]-dx[(d+3) % 4];
y = y + dy[d]-dy[(d+3) % 4];
}
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
cout << num[i][j] << " ";
cout << endl;
}
return 0;
}