3.2.4--迷宫求解

一、题目及分析

求迷宫中从入口到出口的所有路径是一个经典的程序设计问题。由于计算机解决迷宫问题时,通常用的是“穷举求解”的方法,即从口出发,顺某方向向前探索,若能走通,则继续往前走;否则沿原入口路退回,换一个方向再继续探索,直至所有可能的通路都探索到为止。为了保证在任何位置上都能沿原路退回,显然需要用一个后进先出的结构来保存从入口到当前位置的路径。因此,在求迷宫通路的算法中应用“栈”。也就是自然而然的事了。
二、思路
请添加图片描述
请添加图片描述

代码:

结构体构造:

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’ 表示探索过但是不通的路径
下方坐标表示为(行,列)
运行结果:请添加图片描述
这个代码还算是比较有难度的了,算是我写数据结构到现在最难的了,看一遍真没啥感觉,得好好理解。
有无欠缺的,欢迎指正。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ZJJ啥都学不会

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值