#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#define OK 1
#define ERROR 0
typedef int Status;
//迷宫地图(0为通路,1为墙)
int maze[8][8]={
{0,0,1,0,0,0,1,0},
{0,0,1,0,0,0,1,0},
{0,0,0,0,1,1,0,0},
{0,1,1,1,0,0,0,0},
{0,0,0,1,0,0,0,0},
{0,1,0,0,0,1,0,0},
{0,1,1,1,0,1,1,0},
{1,0,0,0,0,0,0,0},
};
//因为要用到坐标,所以先定义坐标的结构体(数据域)
typedef struct{
int x,y;
}Direction;
//定义链表节点的结构体(指针域)
typedef struct Node{
Direction data;
struct Node* next;
}Node;
//定义栈的结构体 将路径放入栈里
typedef struct{
Node* top;
}Stack;
//初始化
Status InitStack(Stack* stack){
stack->top=NULL;
return OK;
}
//入栈
Status Push(Stack* stack,Direction direction){
Node* newNode=(Node*)malloc(sizeof(Node)); //分配空间
newNode->data=direction;
newNode->next=stack->top; //将新结点插入栈顶
stack->top=newNode; //修改栈顶指针
return OK;
}
//出栈
Direction Pop(Stack* stack){
if(stack->top==NULL){
Direction invalidDirection={-1,-1};
return invalidDirection;} //判断是否为空栈
Node* temp=stack->top;
Direction direction=temp->data;
stack->top=temp->next;
free(temp);
return direction;
}
//检查坐标是否有效
bool isValidDirection(Direction direction){
return (direction.x>=0 && direction.x<8 && direction.y>=0 && direction.y<8 )&& maze[direction.x][direction.y]==0;//判断该坐标的位置是否为“0”
}
//打印路径
void printPath(Stack* stack){
Node* currentNode=stack->top;
while(currentNode !=NULL){
printf("(%d,%d)",currentNode->data.x,currentNode->data.y);
currentNode=currentNode->next;
}
printf("\n");
}
//深度优先搜索
bool solveMaze(Direction start,Direction end,Stack* pathStack){
Direction current;
if(!isValidDirection(start)||!isValidDirection(end)||maze[start.x][start.y]==1||maze[end.x][end.y]==1){
return false;
}
Stack backtrackStack; //用于回溯的栈
InitStack(&backtrackStack);
Push(pathStack,start); //将起点入栈
maze[start.x][start.y]=2; //标记起点已访问 将指定位置设置为2
while(pathStack->top !=NULL){
current=pathStack->top->data;
printf("当前探索节点:(%d,%d)\n",current.x,current.y);
if(current.x==end.x && current.y==end.y){
printPath(pathStack);
return OK;
}
bool foundNext=false; //尝试向四个方向移动
for(int i=0;i<4;i++){
Direction next;
switch(i){
case 0://向上移动
next.x=current.x-1;
next.y=current.y;
break;
case 1://向下移动
next.x=current.x+1;
next.y=current.y;
break;
case 2://向左移动
next.x=current.x;
next.y=current.y-1;
break;
case 3://向右移动
next.x=current.x;
next.y=current.y+1;
break;
default:
break;
}
if(isValidDirection(next) && maze[next.x][next.y]==0){
Push(&backtrackStack,next); //入栈待回溯的点
Push(pathStack,next); //入栈路径
maze[next.x][next.y]=2;
foundNext=true;
break;
}
}
if(!foundNext){
//如果没有找到下一步可走的点,回溯到上一步
Direction lastDirection=Pop(&backtrackStack);
maze[current.x][current.y]=0; //标记为未访问
if(backtrackStack.top !=NULL){
//如果回溯栈不为空,将当前位置移动到上一个回溯点
current=Pop(&backtrackStack);
}
}
}
return ERROR;
}
int main(){
int maze[8][8]={0};
Direction start={0,0};
Direction end={7,7};
Stack pathStack;
InitStack(&pathStack);
if(solveMaze(start,end,&pathStack)){
printf("成功走出迷宫!\n");
printPath(&pathStack);
}
else {
printf("未找到路径。\n");
}
return 0;
}
程序一直循环输出“当前搜索节点(6,0)”,不知道怎么修改
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
2条回答 默认 最新
关注 引自免费微信小程序:皆我百晓生
要解决这个问题,首先我们需要了解如何在C语言中处理二维数组,并且理解如何使用栈进行深度优先搜索算法。下面是根据上述要求的解决方案。
解决方案
1. 初始化迷宫地图
首先,我们创建一个二维数组
maze
来表示迷宫的地图。int maze[8][8] = { {0, 0, 1, 0, 0, 0, 1, 0}, {0, 0, 1, 0, 0, 0, 1, 0}, {0, 0, 0, 0, 1, 1, 0, 0}, {0, 1, 1, 1, 0, 0, 0, 0}, {0, 0, 0, 1, 0, 0, 0, 0}, {0, 1, 0, 0, 0, 1, 0, 0}, {0, 1, 1, 1, 0, 1, 1, 0}, {1, 0, 0, 0, 0, 0, 0, 0}, };
2. 定义函数
接下来,我们将定义一些辅助函数,包括检查坐标的有效性、打印路径以及深度优先搜索。
Status isValidDirection(Direction direction) { return (direction.x >= 0 && direction.x < 8 && direction.y >= 0 && direction.y < 8) && maze[direction.x][direction.y] == 0; } bool isGoalReached(Direction start, Direction end) { return start.x == end.x && start.y == end.y; } void printPath(Stack* stack) { Node* currentNode = stack->top; while (currentNode != NULL) { printf("(%d,%d)", currentNode->data.x, currentNode->data.y); currentNode = currentNode->next; } printf("\n"); } bool solveMaze(Direction start, Direction end, Stack* pathStack) { Direction current = start; if (!isValidDirection(current)) { return false; } Stack backtrackStack; initStack(&backtrackStack); push(pathStack, current); maze[current.x][current.y] = 2; // Mark as visited while (pathStack->top != NULL) { current = pathStack->top->data; printCurrentPosition(current); if (isGoalReached(current, end)) { printPath(pathStack); return true; } bool foundNext = false; for (int i = 0; i < 4; ++i) { Direction next = getNextDirection(current, i); if (isValidDirection(next)) { push(&backtrackStack, next); push(pathStack, next); maze[next.x][next.y] = 2; foundNext = true; break; } } if (!foundNext) { popBacktrackStack(); current = popPathStack(); } } return false; } void printCurrentPosition(Direction position) { printf("当前探索节点: (%d, %d)\n", position.x, position.y); } Direction getNextDirection(Direction current, int directionIndex) { switch (directionIndex) { case 0: // Up return Direction{current.x - 1, current.y}; case 1: // Down return Direction{current.x + 1, current.y}; case 2: // Left return Direction{current.x, current.y - 1}; case 3: // Right return Direction{current.x, current.y + 1}; default: throw std::runtime_error("Invalid direction index."); } }
3. 主函数
最后,我们编写主函数并调用
solveMaze
函数来执行深度优先搜索。int main() { int maze[8][8] = { {0, 0, 1, 0, 0, 0, 1, 0}, {0, 0, 1, 0, 0, 0, 1, 0}, {0, 0, 0, 0, 1, 1, 0, 0}, {0, 1, 1, 1, 0, 0, 0, 0}, {0, 0, 0, 1, 0, 0, 0, 0}, {0, 1, 0, 0, 0, 1, 0, 0}, {0, 1, 1, 1, 0, 1, 1, 0}, {1, 0, 0, 0, 0, 0, 0, 0}, }; Direction start = {0, 0}; Direction end = {7, 7}; Stack pathStack; initStack(&pathStack); if (solveMaze(start, end, &pathStack)) { printf("成功走出迷宫!\n"); printPath(&pathStack); } else { printf("未找到路径。\n"); } return 0; }
注意事项:
- 这个实现使用了基本的图形界面操作,例如输入/输出字符串和变量。
- 我们还实现了简单的错误处理和用户友好的提示信息。
- 此示例没有包含对可能的边界条件(例如,当所有入口都被阻挡时)的测试,这是实现正确算法的一个重要部分。
这个解决方案应该可以帮助您解决您的问题。如果您有任何疑问或需要进一步的帮助,请随时提问。
解决 无用评论 打赏 举报 编辑记录
悬赏问题
- ¥50 osgb倾斜摄影模型顶层合并
- ¥60 微信小程序如何上传QQ聊天文件
- ¥300 开发的系统遭到无良商家的破解,请问如何防止再次发生,并追回损失
- ¥15 java代码写在记事本上后在cmd上运行时无报错但又没生成文件
- ¥15 关于#python#的问题:在跑ldsc数据整理的时候一直抱这种错误,要么--out识别不了参数,要么--merge-alleles识别不了参数(操作系统-linux)
- ¥15 PPOCRLabel
- ¥15 混合键合键合机对准标识
- ¥100 现在不懂的是如何将当前的相机中的照片,作为纹理贴图,映射到扫描出的模型上
- ¥15 魔霸ROG7 pro,win11.息屏后会显示黑屏,如图,如何解决?(关键词-重新启动)
- ¥15 有没有人知道这是哪里出了问题啊?要怎么改呀?