一、题目及分析
求迷宫中从入口到出口的所有路径是一个经典的程序设计问题。由于计算机解决迷宫问题时,通常用的是“穷举求解”的方法,即从口出发,顺某方向向前探索,若能走通,则继续往前走;否则沿原入口路退回,换一个方向再继续探索,直至所有可能的通路都探索到为止。为了保证在任何位置上都能沿原路退回,显然需要用一个后进先出的结构来保存从入口到当前位置的路径。因此,在求迷宫通路的算法中应用“栈”。也就是自然而然的事了。
二、思路
代码:
结构体构造:
typedef struct//坐标
{
int x;
int y;
}Coordinate;
typedef struct //栈元素的类型
{
int stepnumber;//通道块在路径上的序号
Coordinate seat;//通道块在迷宫中的坐标
int di;//从此通道块走向下一通道块的方向
}Elem;
typedef struct //栈
{
Elem* top;
Elem* base;
int stacksize;
}Stack;
typedef struct //地图类型
{
int length;
int width;
int mapdic[MAXZISE][MAXZISE];
}Maptype;
栈的常规操作:
int InitStack(Stack* s)//栈初始化
{
s->base = (Elem*)malloc(STACK_INIT_SIZE * sizeof(Elem));
if(!s->base) return 0;
s->top = s->base;
s->stacksize = STACK_INIT_SIZE;
return 1;
}
int Push(Stack* s, Elem* e)//压栈
{
if (s->top - s->base >= s->stacksize)
{
s->base = (Elem*)realloc(s->base, (STACK_INIT_SIZE + STACKINCREMENT) * sizeof(Elem));
s->top = s->base + s->stacksize;
s->stacksize += STACKINCREMENT;
}
*s->top = *e;
s->top++;
return 1;
}
int Pop(Stack* s, Elem* e)//出栈
{
if (s->base == s->top)
{
cout << "空栈,无法出栈" << endl;
return 0;
}
s->top--;
*e = *s->top;
return 1;
}
路径打印:
int MapPath(Maptype map, Coordinate start, Coordinate end)//若迷宫有通路,则打印结果
{
Stack s;
Elem e;
InitStack(&s);
Coordinate coord = start;
int stepnum = 1;
do {
if (Pass(map, coord))//如果可以通过
{
FootPrint(&map, coord);
e = CreatElem(stepnum, coord, 1);
Push(&s, &e);
if (End(coord, end))
{
system("CLS");
PrintMap(&map, ROW, COL);
return 1;
}
else
{
coord = NextPod(coord, 1);
stepnum++;
}
}
else//如果不能通过
{
if (!StackEmpty(&s)) {
Pop(&s, &e);
while (e.di == 4 && !StackEmpty(&s))
{
MarkPrint(&map,e.seat);
Pop(&s, &e);
}
if (e.di < 4) {
e.di++;
Push(&s, &e);
coord = NextPod(e.seat, e.di);
}
}
}
Sleep(100);
system("CLS");
PrintMap(&map, ROW, COL);
} while (!StackEmpty(&s));
return 0;
}
总代码:
#include<iostream>//ZJJ数据据结构-栈3.2.4
#include <windows.h>
using namespace std;
#define MAXZISE 100
#define ROW 10//行数
#define COL 10//列数
#define STACK_INIT_SIZE 100//迷宫最大范围
#define STACKINCREMENT 10
typedef struct//坐标
{
int x;
int y;
}Coordinate;
typedef struct //栈元素的类型
{
int stepnumber;//通道块在路径上的序号
Coordinate seat;//通道块在迷宫中的坐标
int di;//从此通道块走向下一通道块的方向
}Elem;
typedef struct //栈
{
Elem* top;
Elem* base;
int stacksize;
}Stack;
typedef struct //地图类型
{
int length;
int width;
int mapdic[MAXZISE][MAXZISE];
}Maptype;
int InitStack(Stack* s)//栈初始化
{
s->base = (Elem*)malloc(STACK_INIT_SIZE * sizeof(Elem));
if(!s->base) return 0;
s->top = s->base;
s->stacksize = STACK_INIT_SIZE;
return 1;
}
int Push(Stack* s, Elem* e)//压栈
{
if (s->top - s->base >= s->stacksize)
{
s->base = (Elem*)realloc(s->base, (STACK_INIT_SIZE + STACKINCREMENT) * sizeof(Elem));
s->top = s->base + s->stacksize;
s->stacksize += STACKINCREMENT;
}
*s->top = *e;
s->top++;
return 1;
}
int Pop(Stack* s, Elem* e)//出栈
{
if (s->base == s->top)
{
cout << "空栈,无法出栈" << endl;
return 0;
}
s->top--;
*e = *s->top;
return 1;
}
Elem CreatElem(int stepnumber, Coordinate seat, int di)//构造栈的元素
{
Elem e;
e.di = di;
e.seat = seat;
e.stepnumber = stepnumber;
return e;
}
int InitMap(Maptype* map, int a[ROW][COL], int row, int col)//构建迷宫
{
for (int i = 0;i < row;i++)
{
for (int j = 0;j < col;j++)
{
map->mapdic[i][j] = a[i][j];//将自己用数字表示的迷宫传入
}
}
map->length = row;
map->width = col;
return 1;
}
int Pass(Maptype map, Coordinate coord)//判断是否通过
{
return map.mapdic[coord.x][coord.y] == 0;
}
int FootPrint(Maptype* map, Coordinate coord)//若通过,标记为O
{
map->mapdic[coord.x][coord.y] = 'O';
return 1;
}
int MarkPrint(Maptype* map, Coordinate coord)//若不能通过,标记为X
{
map->mapdic[coord.x][coord.y] = 'X';
return 1;
}
int StackEmpty(Stack* s)//判断是否为空栈
{
return s->base == s->top;
}
int End(Coordinate coord, Coordinate end)//判断当前坐标是否为终点坐标
{
if (coord.x == end.x && coord.y == end.y)
return 1;
else return 0;
}
Coordinate NextPod(Coordinate coord, int di)//下一步的方向
{
switch (di)
{
case 1:
coord.y++;
break;
case 2:
coord.x++;
break;
case 3:
coord.y--;
break;
case 4:
coord.x--;
break;
}
return coord;
}
int PrintMap(Maptype* map, int row, int col)
{
for (int i = 0;i < row;i++) {
for (int j = 0;j < col;j++) {
switch (map->mapdic[i][j]) {
case 0:
cout << ' '; //未探索
break;
case 1:
cout << '#'; //迷宫的墙
break;
case 'O':
cout << 'O'; //通过的路径
break;
case 'X':
cout << 'X'; //探索后不能通过的路径
}
}cout << endl;
}
cout << " " << "start:";
for (int i = 0;i < row;i++)
{
for (int j = 0;j < col;j++)
{
if (map->mapdic[i][j] == 'O')
{
cout << "(" << i << "," << j << ")" << "->";
}
}
}
cout << "end" << endl;
return 1;
}
int MapPath(Maptype map, Coordinate start, Coordinate end)//若迷宫有通路,则打印结果
{
Stack s;
Elem e;
InitStack(&s);
Coordinate coord = start;
int stepnum = 1;
do {
if (Pass(map, coord))//如果可以通过
{
FootPrint(&map, coord);
e = CreatElem(stepnum, coord, 1);
Push(&s, &e);
if (End(coord, end))
{
system("CLS");
PrintMap(&map, ROW, COL);
return 1;
}
else
{
coord = NextPod(coord, 1);
stepnum++;
}
}
else//如果不能通过
{
if (!StackEmpty(&s)) {
Pop(&s, &e);
while (e.di == 4 && !StackEmpty(&s))
{
MarkPrint(&map,e.seat);
Pop(&s, &e);
}
if (e.di < 4) {
e.di++;
Push(&s, &e);
coord = NextPod(e.seat, e.di);
}
}
}
Sleep(100);
system("CLS");
PrintMap(&map, ROW, COL);
} while (!StackEmpty(&s));
return 0;
}
int main()
{
int a[ROW][COL] =
{ {1,1,1,1,1,1,1,1,1,1} ,
{1,0,0,1,0,0,0,1,0,1 },
{1,0,0,1,0,0,0,1,0,1 },
{1,0,0,0,0,1,1,0,0,1 },
{1,0,1,1,1,0,0,0,0,1 },
{1,0,0,0,1,0,0,0,0,1 },
{1,0,1,0,0,0,1,0,0,1 },
{1,0,1,1,1,0,1,1,0,1 },
{1,1,0,0,0,0,0,0,0,1 },
{1,1,1,1,1,1,1,1,1,1} };
Maptype map;//构建地图
Coordinate start, end;//定义初始坐标
start.x = start.y=1;//定义出口坐标
end.x = end.y =8;//定义终点坐标
InitMap(&map, a, ROW, COL);
if (!MapPath(map, start, end))
{
cout << "NO PASS";
}
return 0;
}
表示通过的路径,‘X’ 表示探索过但是不通的路径
下方坐标表示为(行,列)
运行结果:
这个代码还算是比较有难度的了,算是我写数据结构到现在最难的了,看一遍真没啥感觉,得好好理解。
有无欠缺的,欢迎指正。