El código está adaptado del algoritmo del Capítulo 2 de la edición [Estructura de datos] de Yan Weimin, la mayor parte del cual también está tomado prestado de
Haga clic para abrir el enlace http://www.cnblogs.com/kangjianwei101/p/5221816.html;
Lo que es particularmente extraño y consume la mayor parte de mi tiempo es que el mismo fragmento de código no muestra múltiples marcadores de "callejones sin salida/punto muerto" y finaliza automáticamente después de compilar y ejecutar Dev C++. Cambie a bloques de código para abrir el proyecto, y se compilará y ejecutará con total normalidad. Xiaobai no entendió por qué, pero fue realmente maravilloso.
El principio es crear una estructura de datos abstracta de una pila y almacenar la ruta recorrida en la pila. Cuando no haya camino por recorrer, aparece un elemento y continúa explorando otras direcciones hasta que la pila esté vacía o el laberinto salga.
C Principal
#include "Maze.h"
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char *argv[]) {
MazeType maze[N][N];
PosType start,end;
// SElemType_Sq e;
char again='y';
int flag=0;
while (again=='y')
{
InitMaze(maze,&start,&end);
ShowMaze(maze);
flag = MazePath(maze,start,end);
if (flag==OK){
printf("\n We made it!!!");
} else {
printf("\n Mission failed.");
}
printf("Wanna try again?(y/n)");
scanf("%c",&again);
}
return 0;
}
laberinto.h
#ifndef Maze_h
#define Maze_h
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "Status.h"
#include "SequenceStack.h"
//宏定义
#define N 15 //迷宫的大小
#define X 4 //用于随机取余,X越大生成迷宫的通行概率越大
#define SleepTime 2 //代表休眠时间
//迷宫方块的类型定义
typedef enum
{
Wall,Obstacle,Way,DeadLock, //路径上的死胡同
East,South,West,North,
Start,End,
} MazeNode;
迷宫的通道块坐标 注意:x表示从坐标,y表示横坐标
//typedef struct
//{
// int x;
// int y;
//} PosType;
通道块信息
//typedef struct
//{
// int ord; //通道快序列号
// PosType seat; //坐标位置
// int di; //下一个该访问的方向 东4 南5 西6 北7
//}SElemType_Sq;
typedef int MazeType; //迷宫元素类型
//函数表
//穷举法的算法
Status MazePath (MazeType maze[][N],PosType start,PosType end);
//初始化
void InitMaze(MazeType maze[][N], PosType *start,PosType *end);
//在屏幕上画出迷宫
void PaintMaze(MazeType[][N]);
//显示迷宫
void ShowMaze (MazeType[][N]);
//比较是否为为同一通道块
Status EqualPosType (PosType a,PosType b);
//判定此通道快是否未访问
Status Passable(PosType seat,MazeType maze[][N]);
//留下初始足迹
void FootPrint (PosType seat,MazeType maze[][N]);
//更新通道快信息
void SetSElemType (SElemType_Sq *e,int ord,PosType seat,int di);
//当前通道快的后继
PosType NextPos (PosType seat, int di);
// 判断是否越界
Status IsCross(PosType seat);
//标记不可访问
void MarkDeadLock(PosType seat,MazeType maze[][N]);
#endif
laberinto.c
/*顺序栈操作头文件*/
#ifndef Maze_c
#define Maze_c
#include "Maze.h"
//函数表
//穷举法的算法
Status MazePath (MazeType maze[][N],PosType start,PosType end)
{
SqStack S; //存入步数的栈模型
SElemType_Sq node; //栈内迷宫格子的存储单位
PosType curPos; //现在的坐标
int curStep; //
InitStack_Sq(&S);
curPos = start;
curStep = 1;
//求得线路即放入栈中
do {
if (Passable(curPos,maz