#pragma once#include<stdio.h>#include"Stack.h"#define MAX_ROW 6#define MAX_COL 6typedefstruct position
{int x;int y;}position;typedefstruct Maze
{int map[MAX_ROW][MAX_COL];}Maze;voidInitMap(Maze* m,int map[MAX_ROW][MAX_COL]);voidPrintMap(Maze* m);voidPassMap(Maze* m, position enter, Stack* s);//走迷宫voidPassMapRec(Maze * m, position enter);//走迷宫,递归走法int_PassMaze(Maze* m, position entry, position cur);//辅助递归intIsValidEnter(Maze* m, position enter);//检测迷宫的入口是否有效intIsMazeExit(Maze* m, position cur, position enter);//检测cur的位置是否为迷宫的出口intIsPass(Maze* m, position cur);//检测该位置是否通路voidPrintPath(Stack* s);//打印走过的路径
Maze.c
#include"Maze.h"voidInitMap(Maze * m,int map[MAX_ROW][MAX_COL]){int i =0, j =0;if(NULL== m){return;}for(i =0; i < MAX_ROW; i++){for(j =0; j < MAX_COL; j++){
m->map[i][j]= map[i][j];}}}voidPrintMap(Maze * m){int i =0, j =0;if(NULL== m){return;}for(i =0; i < MAX_ROW; i++){for(j =0; j < MAX_COL; j++){printf("%d ", m->map[i][j]);}printf("\n");}}voidPassMap(Maze * m, position enter, Stack * s){//保存路径->走当前步//1.检测入口是否合法(非法入口->返回;合法入口->保存入口->stack)
position next;//当前位置下一步该走的坐标if(!IsValidEnter(m, enter)){printf("迷宫入口有误!!!\n");return;}//2.取栈顶位置cur->走cur位置->检测cur是否为出口(IsExit( ),是出口->返回)//先检测next位置是否可以走通//可以走通->保存当前路径StackPush(s, enter);while(StackEmpty(s)){
position cur =StackTop(s);//取栈顶元素,作为每次的入口
m->map[cur.x][cur.y]=2;//走过就标记为2if(IsMazeExit(m, cur,enter))//已经走到出口就退出{return;}//上
next = enter;
next.x -=1;if(Ispass(m, next)){StackPush(s, next);continue;}//左
next = cur;
next.y -=1;if(Ispass(m, next)){StackPush(s, next);continue;}//右
next = cur;
next.y +=1;if(Ispass(m, next)){StackPush(s, next);continue;}//下
next = cur;
next.x +=1;if(Ispass(m, next)){StackPush(s, next);continue;}//说明cur步走错了->重新标记->出栈
m->map[cur.x][cur.y]=3;StackPop(s);}}voidPassMapRec(Maze * m, position enter){//保存路径->走当前步//1.检测入口是否合法(非法入口->返回;合法入口->保存入口->stack)if(!IsValidEnter(m, enter)){printf("迷宫入口有误!!!\n");return;}_PassMaze(m, enter, enter);}intIsValidEnter(Maze * m, position enter){if(NULL== m){return;}//保证入口一定在边界if(0== enter.x || MAX_ROW -1== enter.x ||0== enter.y || MAX_COL -1== enter.y){return1== m->map[enter.x][enter.y];//有效的入口返回1,无效返回0}return0;}intIsMazeExit(Maze * m, position cur, position enter){if(cur.x == enter.x && cur.y == enter.y){return0;}if(cur.x ==0|| cur.x == MAX_ROW -1|| cur.y ==0|| cur.y == MAX_COL -1){return1;}return0;}intIsPass(Maze * m, position cur){return m->map[cur.x][cur.y]==1;}voidPrintPath(Stack * s){//当栈的元素个数大于一,取出栈顶元素,打印,出一个栈顶元素
position top;while(StackSize(s)>1){
top =StackTop(s);printf("{%d, %d}<---", top.x, top.y);StackPop(s);}//这时打印最后一个元素
position top =StackTop(s);printf("{%d, %d}", top.x, top.y);}int_PassMaze(Maze * m, position entry, position cur){if(IsPass(m, cur)){
position next;
m->map[cur.x][cur.y]=2;if(IsMazeExit(m, cur, entry)){return1;}//上
next = cur;
next.x -=1;if(_PassMaze(m, entry, next)){return1;}//左
next = cur;
next.y -=1;if(_PassMaze(m, entry, next)){return1;}//右
next = cur;
next.y +=1;if(_PassMaze(m, entry, next)){return1;}//下
next = cur;
next.x +=1;if(_PassMaze(m, entry, next)){return1;}
m->map[cur.x][cur.y]=3;}return0;}