Files
python/GESP/资料/SheXingJuZhen.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

31 lines
1.2 KiB
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <bits/stdc++.h>
using namespace std;
const int N = 110;
int dx[] = {0, 1, 0, -1}; // 右下左上
int dy[] = {1, 0, -1, 0}; // 右下左上
int a[N][N];
int x, y, p; // x,y:当前蛇走到的坐标位置p现在正在使用哪个方向
int n, m;
int main() {
cin >> n >> m;
for (int i = 1; i <= n * m; i++) { // 没有填充完毕
a[x][y] = i; // 在当前位置填充数字, 值在长大
// p是说方向数组中的游标它是需要变化的什么情况下变化呢
// 1、出界了就变化 2、遇到了障碍就变化
int tx = x + dx[p], ty = y + dy[p]; // 蛇准备去的位置
if (tx >= n || ty >= m || tx < 0 || ty < 0 || a[tx][ty])
p = (p + 1) % 4; // 换下个方向(本题比较容易,因为在没有填充完时,每次碰壁只需换下个方向就一定有可以走的路)
// 下一个坐标位置 : 注意:下面的代码不能用 x=tx,y=ty!!! 原因是经过上面的转换p可能变化了!!!
x += dx[p], y += dy[p];
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++)
printf("%3d", a[i][j]);
cout << endl;
}
return 0;
}