求大神帮忙写一个求解迷宫的程序,用栈实现就可以
求解迷宫,用栈实现,最后输出路线坐标和方向就好#include"List.h" #include<windows.h> #include<conio.h> #ifndef MAXSIZE #define MAXSIZE 10 //迷宫的最大长度 #endif typedef char MAP; //自定义迷宫地图 typedef enum Visit { DISCOVER, COVER, }Visit; typedef enum DIRECTION //枚举四个方向 { UP, DOWN, LEFT, RIGHT, }DIRECTION; typedef struct Point //定义坐标对 { int x; //横坐标 int y; //纵坐标 }Point; typedef struct Graph //储存迷宫地图结构体 { Visit visit[sizeof(DISCOVER)]; Visit visit_This; MAP data; //记录储存数据 }Graph; typedef struct Maze //迷宫信息 { Point start; //起点坐标 Point end; //终点坐标 int d; Graph graph[MAXSIZE][MAXSIZE]; //迷宫结构信息 }Maze,*PMaze; typedef int MAZE_MOVE(PMaze maze,Point* point); //自定义变量 void Maze_Init(PMaze maze,MAP map[][MAXSIZE+1]); //初始化迷宫 void Maze_Print(PMaze maze); //打印迷宫 void Maze_Print_Road(PList p,PMaze maze); //打印路 void Maze_Find_Road(PMaze maze); //寻找迷宫出口路径 MAZE_MOVE Maze_Move_Up; //对四个方向的移动进行判断 MAZE_MOVE Maze_Move_Down; MAZE_MOVE Maze_Move_Left; MAZE_MOVE Maze_Move_Right; MAZE_MOVE Maze_Judge_Win; //判断是否找到终点 void Print_COM(void* p); //自定义打印函数 int main() { Maze maze={0}; MAP map[MAXSIZE][MAXSIZE+1]= { {"**********"}, {"* ** *"}, {"* *** ** *"}, {"* ** * *"}, {"* ** * *"}, {"* ** * *"}, {"* ** *"}, {"* ****** *"}, {"* ***** *"}, {"**********"}, }; system("cls"); Maze_Init(&maze,map); Maze_Print(&maze); Maze_Find_Road(&maze); return 0; } void Maze_Init(PMaze maze,MAP map[][MAXSIZE+1]) //初始化迷宫 { int i=0; int j=0; int k=0; maze->start.x=1; //初始化起点坐标 maze->start.y=1; maze->end.x=8; //初始化终点坐标 maze->end.y=8; maze->d=sizeof(DISCOVER); for (i=0;i<MAXSIZE;++i) for (j=0;j<MAXSIZE;++j) { maze->graph[i][j].data=map[i][j]; for (k=0;k<maze->d;++k) maze->graph[i][j].visit[k]=DISCOVER; maze->graph[i][j].visit_This=DISCOVER; //表示该格没有走过 } } void Maze_Print(PMaze maze) { int i=0; int j=0; for (i=0;i<MAXSIZE;++i) { for (j=0;j<MAXSIZE;++j) putchar(maze->graph[i][j].data); puts(""); } } void Maze_Find_Road(PMaze maze) //寻找迷宫出口路径 { PList p=NULL; Point point= //获取起点坐标 { maze->start.x, maze->start.y, }; MAZE_MOVE *move[sizeof(DISCOVER)]={0}; //定义四个方向的指针函数数组 int win=0; //判断是否有出口 move[UP]=Maze_Move_Up; move[DOWN]=Maze_Move_Down; move[LEFT]=Maze_Move_Left; move[RIGHT]=Maze_Move_Right; List_Fun.Creat_Link(&p,&point,sizeof(Point)); //创建一个栈 List_Fun.Insert_Rear(p,&point); //起始坐标入栈 maze->graph[point.x][point.y].visit_This=COVER; //标记此坐标被遍历过 printf("起点坐标:(%d,%d)\n",point.x,point.y); printf("终点坐标:(%d,%d)\n",maze->end.x,maze->end.y); while (!List_Fun.Empty_List(p)) { int i=0; int len=sizeof(DISCOVER); for (i=0;i<len;++i) { if (move[i](maze,&point)&&((win=Maze_Judge_Win(maze,&point))==0)) { List_Fun.Insert_Rear(p,&point); //入栈 i=-1; //对新位置重新判断 } if (win==1) { List_Fun.Insert_Rear(p,&point); break; } } if (win==1) { puts("深度搜索路线如下:"); List_Fun.Println_List(p,Print_COM); //输出链表数据 break; } List_Fun.Del_Rear(p,&point); //出栈 } if (List_Fun.Empty_List(p)) puts("找不到出口!"); else printf("共走了%d步\n",p->length); while (!List_Fun.Empty_List(p)) { List_Fun.Remove_Front(p,&point); maze->graph[point.x][point.y].data='o'; } if (win) { puts("请按任意键输出迷宫路线:"); getch(); Maze_Print(maze); } List_Fun.Del_List(&p,1); //释放栈 } int Maze_Move_Up(PMaze maze,Point* point) //自定义变量 { if (maze->graph[point->x][point->y].visit[UP]==COVER) return 0; if (point->x-1<0||maze->graph[point->x-1][point->y].data=='*') return 0; maze->graph[point->x][point->y].visit[UP]=COVER; maze->graph[point->x][point->y].visit_This=COVER; --point->x; maze->graph[point->x][point->y].visit[DOWN]=COVER; //对返回路径进行处理 return 1; } int Maze_Move_Down(PMaze maze,Point* point) //自定义变量 { if (maze->graph[point->x][point->y].visit[DOWN]==COVER) return 0; if (point->x+1>MAXSIZE-1||maze->graph[point->x+1][point->y].data=='*'||maze->graph[point->x+1][point->y].visit_This==COVER) return 0; maze->graph[point->x][point->y].visit[DOWN]=COVER; maze->graph[point->x][point->y].visit_This=COVER; ++point->x; maze->graph[point->x][point->y].visit[UP]=COVER; return 1; } int Maze_Move_Left(PMaze maze,Point* point) //自定义变量 { if (maze->graph[point->x][point->y].visit[LEFT]==COVER) return 0; if (point->y-1<0||maze->graph[point->x][point->y-1].data=='*'||maze->graph[point->x][point->y-1].visit_This==COVER) return 0; maze->graph[point->x][point->y].visit[LEFT]=COVER; maze->graph[point->x][point->y].visit_This=COVER; --point->y; maze->graph[point->x][point->y].visit[RIGHT]=COVER; return 1; } int Maze_Move_Right(PMaze maze,Point* point) //自定义变量 { if (maze->graph[point->x][point->y].visit[RIGHT]==COVER) return 0; if (point->y+1>MAXSIZE-1||maze->graph[point->x][point->y+1].data=='*'||maze->graph[point->x][point->y+1].visit_This==COVER) return 0; maze->graph[point->x][point->y].visit[RIGHT]=COVER; maze->graph[point->x][point->y].visit_This=COVER; ++point->y; maze->graph[point->x][point->y].visit[LEFT]=COVER; return 1; } int Maze_Judge_Win(PMaze maze,Point* point) //自定义变量 { if (maze->end.x==point->x&&maze->end.y==point->y) { return 1; } return 0; } void Print_COM(void* p) //自定义打印函数 { int x=((Point* )p)->x; int y=((Point* )p)->y; printf("%-4d%-4d\n",x,y); }
[此贴子已经被作者于2017-6-4 21:55编辑过]