c语言先建造一个迷宫 “u”表示向上走“d”表示向下走 “l”表示向左走“r”表示向右走 做后用“u”“d”“l”“r”表示出路径
时间: 2023-07-24 11:12:18 浏览: 44
好的,我可以给你一些思路和代码示例。
首先,我们可以使用二维数组来表示迷宫,其中 0 表示可走的路径,1 表示墙壁。例如,下面是一个简单的迷宫示例:
```
int maze[5][5] = {
{0, 1, 0, 0, 0},
{0, 1, 0, 1, 0},
{0, 0, 0, 0, 0},
{0, 1, 1, 1, 0},
{0, 0, 0, 1, 0}
};
```
接下来,我们可以使用递归函数来搜索迷宫,并记录路径。具体来说,我们可以定义一个函数 `findPath`,它接受当前位置 `(x, y)` 和目标位置 `(targetX, targetY)` 作为参数,返回一个字符串表示从当前位置到目标位置的路径。在函数内部,我们可以按照上、下、左、右的顺序依次尝试移动,如果移动后的位置是合法的并且没有被访问过,我们就递归调用 `findPath` 函数,并将返回值加入到当前位置的路径中。最后,我们返回从当前位置到目标位置的路径。
下面是完整的代码示例:
```
#include <stdio.h>
#include <string.h>
int maze[5][5] = {
{0, 1, 0, 0, 0},
{0, 1, 0, 1, 0},
{0, 0, 0, 0, 0},
{0, 1, 1, 1, 0},
{0, 0, 0, 1, 0}
};
char* findPath(int x, int y, int targetX, int targetY, char* path) {
if (x < 0 || x >= 5 || y < 0 || y >= 5 || maze[x][y] == 1) {
// 当前位置越界或者是墙壁,返回空路径
return "";
}
if (x == targetX && y == targetY) {
// 到达目标位置,返回当前位置
char* result = (char*) malloc(strlen(path) + 2);
sprintf(result, "%s(%d,%d)", path, x, y);
return result;
}
// 尝试向上走
char* result = findPath(x - 1, y, targetX, targetY, path);
if (strlen(result) > 0) {
// 向上走可以到达目标位置
char* newPath = (char*) malloc(strlen(path) + 2 + strlen(result));
sprintf(newPath, "%s(%d,%d)%s", path, x, y, result);
free(result);
return newPath;
}
// 尝试向下走
result = findPath(x + 1, y, targetX, targetY, path);
if (strlen(result) > 0) {
// 向下走可以到达目标位置
char* newPath = (char*) malloc(strlen(path) + 2 + strlen(result));
sprintf(newPath, "%s(%d,%d)%s", path, x, y, result);
free(result);
return newPath;
}
// 尝试向左走
result = findPath(x, y - 1, targetX, targetY, path);
if (strlen(result) > 0) {
// 向左走可以到达目标位置
char* newPath = (char*) malloc(strlen(path) + 2 + strlen(result));
sprintf(newPath, "%s(%d,%d)%s", path, x, y, result);
free(result);
return newPath;
}
// 尝试向右走
result = findPath(x, y + 1, targetX, targetY, path);
if (strlen(result) > 0) {
// 向右走可以到达目标位置
char* newPath = (char*) malloc(strlen(path) + 2 + strlen(result));
sprintf(newPath, "%s(%d,%d)%s", path, x, y, result);
free(result);
return newPath;
}
// 四个方向都不能到达目标位置,返回空路径
return "";
}
int main() {
char* path = findPath(0, 0, 4, 4, "");
printf("%s\n", path);
free(path);
return 0;
}
```
在上面的例子中,我们从起点 `(0, 0)` 开始搜索迷宫,目标位置为 `(4, 4)`。最终输出的路径为 `(0,0)(1,0)(2,0)(2,1)(2,2)(2,3)(1,3)(0,3)(0,4)(1,4)(2,4)(3,4)(4,4)`,表示从起点到目标位置的路径。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)