#include <cstdio>
using namespace std;
const int N = 1e4+10;
int n, m;
int q[N][N];
int main()
{
scanf("%d %d", &n, &m);
int dx[] = {-1, 0, 1, 0}, dy[] = {0,1,0,-1};
int d = 1, x = 0, y = 0;
for(int k = 1; k <= n * m; k++)
{
q[x][y] = k;
int a = x + dx[d], b = y + dy[d];
if(a < 0 || a >= n || b < 0 || b >= m || q[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++)
printf("%d ", q[i][j]);
puts("");
}
return 0;
}
蛇形矩阵~
最新推荐文章于 2024-08-28 10:02:11 发布