diff --git a/GESP/SheXingJuZhen.cpp b/GESP/SheXingJuZhen.cpp new file mode 100644 index 0000000..4564fb7 --- /dev/null +++ b/GESP/SheXingJuZhen.cpp @@ -0,0 +1,31 @@ +#include +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 k = 1; k <= n * m; k++) { // 没有填充完毕 + a[x][y] = k; // 在当前位置填充数字, 值在长大 + // 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; +} \ No newline at end of file diff --git a/GESP/Test.cpp b/GESP/Test.cpp deleted file mode 100644 index bb100b6..0000000 --- a/GESP/Test.cpp +++ /dev/null @@ -1,11 +0,0 @@ -#include -using namespace std; - -int main() { - for (int a = INT_MIN; a <= INT_MAX; a++) { - if (a * 2 / 2 != a) - cout << a << endl; - } - - return 0; -} \ No newline at end of file