用栈 求迷宫问题(最短路径和全部路径)

4 篇文章 0 订阅
这是数据结构的作业,便找书边看网上,然后自己慢慢写出来的,这里面主要是回溯法。
因为课本上是打印出一条路径,然后我在想怎样能将所有的路径都输出来,方法:就是当求出一条路径后,将出口点变成可以走的点(因为之前将其值变成了-1),并且将栈顶元素出栈,还需要得到现在栈顶元素的i,j,di值,将其赋出来。
这里的思路是这样的,因为找最后一个点的时候是找倒数第二个点的上下左右四个方位,假设说是路径通畅的点是倒数第二个点的上面的点,当我们找另一条路径时,那么现在我们应该顺时针从次栈顶右面的点试探,查看是不是可行的点。


#include <stdio.h>
#define Max 9999
int mg[6][6] = {
			  {1,1,1,1,1,1},{1,0,0,0,1,1},
			  {1,0,1,0,0,1},{1,0,0,0,1,1},
			  {1,1,0,0,0,1},{1,1,1,1,1,1}
};
typedef struct {
	int i;
	int j;
	int di;
}BOX;

typedef struct {
	BOX data[Max];
	int top;
}SqStack;

int InitSqStack(SqStack *s)
{
	s->top = -1;
	return 0;
}

int Destroy(SqStack *s)
{
	free(s);
	return 0;
}
int Push(SqStack *s, BOX *e)
{
	if (s->top == Max - 1)
		return 0;
	s->top++;
	s->data[s->top] = *e;
	return 0;
}
int GetTop(SqStack *s, BOX *e)
{
	if (s->top == -1)
		return 1;
	*e = s->data[s->top];
	return 0;
}
int Pop(SqStack *s, BOX *e)
{
	if (s->top == -1)
		return 1;
	*e = s->data[s->top];
	s->top--;
	return 0;
}
int Empty(SqStack *s)
{
	if (s->top == -1)
		return 1;
	else
		return 0;
}//1是空,0是非空。
int Man(SqStack *s)
{
	if (s->top == Max - 1)
		return 1;
	else
		return 0;
}



int zmg(int x, int y, int xe, int ye)
{
	SqStack S;
	BOX path[Max], e, E;
	BOX shortpath[Max];
	int di = 0, x1 = 0, y1 = 0, i = 0, j = 0;
	int k = 0, fun = 0;
	int m = 0, lm = 0, n = 0, op = 0;
	int minlen = Max;
	int js = 0, tp = 0;
	InitSqStack(&S);//初始化栈
	e.i = x;
	e.j = y;
	e.di = -1;
	Push(&S, &e);
	mg[x][y] = -1;
	while (Empty(&S) != 1)//不等于空
	{
		GetTop(&S, &e);
		i = e.i;
		j = e.j;
		di = e.di;
		if (i == xe && j == ye)
		{
			k = 0;
			tp = S.top;
			for (js = 0;js <= tp;js++)
			{
				S.top = js;
				printf("\t(%d,%d)", S.data[S.top].i, S.data[S.top].j);
				shortpath[n] = path[op];
			}
			printf("\n");
			
			if (tp + 1 < minlen)
			{	
				for (S.top = 0,n = 0;n <=tp;n++,S.top++)
				{
					shortpath[n] = S.data[S.top];		
				}
				S.top = tp;
				minlen = tp + 1;
			}
			mg[i][j] = 0;
			Pop(&S, &e);
			GetTop(&S, &e);
			i = e.i;
			j = e.j;
			di = e.di;
		}
		fun = 0;
		while (di < 4 && fun != 1)
		{
			di++;
			switch (di)
			{
			case 0:
				x1 = i - 1;
				y1 = j;
				break;
			case 1:
				x1 = i;
				y1 = j + 1;
				break;
			case 2:
				x1 = i + 1;
				y1 = j;
				break;
			case 3:
				x1 = i;
				y1 = j - 1;
				break;
			}
			if (mg[x1][y1] == 0)
				fun = 1;
		}
		if (fun == 1)
		{
			S.data[S.top].di = di;
			e.i = x1;
			e.j = y1;
			e.di = -1;
			Push(&S, &e);
			mg[x1][y1] = -1;
		}
		else
		{
			Pop(&S, &e);
			mg[e.i][e.j] = 0;
		}
	}
	      printf("\n");
	      printf("最短路径的长度%d,M:",minlen);
	      for(n=0;n<minlen;n++)
	     {
	             printf("\t(%d,%d)",shortpath[n].i,shortpath[n].j);
	    }
	      Destroy(&S);
	return 0;
}
int main()
{
	printf("迷宫所有路径如下:\n");
	zmg(1, 1, 4, 4);
	return 0;
}

 

 

 

 

 

 

 

问题描述: 以一个m*n的长方阵表示迷宫,0和1分别表示迷宫中的通路和障碍。设计一个程序,对任意设定的迷宫,出从入口(0,0)到出口(m-1,n-1)的通路和通路总数,或得出没有通路的论。例如下图, 0(入口) 1 0 1 0 0 0 0 1 0 0 1 1 0 0 0 0 0 0 0 0 1 0 0 0(出口) 从入口到出口有6条不同的通路。 而下图: 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 0 0 1 1 0 1 0 1 1 1 0 0 1 0 0 0 0 1 0 0 0 0 0 1 0 0 0 1 0 1 0 1 1 1 1 0 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 0 0 0 从入口到出口则没有通路。 算法设计: 给定一个m*n的长方阵表示迷宫,设计算法输出入口到出口的通路和通路总数,或得出没有通路的论。 算法提示: 和皇后问题与分书问题类似。可以用二维数组存储迷宫数据,对于迷宫中任一位置,均可约定有东、南、西、北四个方向可通。从当前位置a(用(x,y)表示一个位置,假定它是以向右的x轴和向下的y轴组成的平面上的一个点)出发依次尝试四个方向是否有路,若某个方向的位置b可通,则按照同样的方法继续从b出发寻找。若到达出口,则找到一条通路。 数据输入: 由文件input.txt 提供输入数据。第一行是m和n的值,空格分隔,其后共m行。每行有n个数字,数和数之间用空格分隔。 果输出: 将计算出的所有从入口到出口的通路输出到文件output.txt 中。若没有通路,则将0写入文件中。
以下是使用栈求解迷宫问题最短路径的代码实现(假设迷宫是由 0 和 1 组成的二维数组,其中 0 表示通路,1 表示障碍): ``` #include <iostream> #include <stack> using namespace std; const int MAXSIZE = 100; struct Pos { int x, y; }; int maze[MAXSIZE][MAXSIZE]; // 迷宫 int visited[MAXSIZE][MAXSIZE]; // 记录是否访问过 int m, n; // 迷宫的行数和列数 stack<Pos> s; // 存储路径的栈 // 判断一个位置是否可以访问 bool isAccessible(int x, int y) { if (x < 0 || x >= m || y < 0 || y >= n) // 越界 return false; if (maze[x][y] == 1 || visited[x][y] == 1) // 障碍或已访问过 return false; return true; } // 求解迷宫问题最短路径 void solveMaze(int startX, int startY, int endX, int endY) { s.push({startX, startY}); // 将起点入栈 visited[startX][startY] = 1; // 标记起点已访问过 while (!s.empty()) { Pos currPos = s.top(); s.pop(); // 如果到达终点,输出路径并退出 if (currPos.x == endX && currPos.y == endY) { cout << "Shortest path: "; while (!s.empty()) { Pos p = s.top(); cout << "(" << p.x << "," << p.y << ") "; s.pop(); } cout << "(" << endX << "," << endY << ")" << endl; return; } // 将当前位置的相邻可访问位置入栈 if (isAccessible(currPos.x, currPos.y-1)) { // 左 s.push({currPos.x, currPos.y-1}); visited[currPos.x][currPos.y-1] = 1; } if (isAccessible(currPos.x, currPos.y+1)) { // 右 s.push({currPos.x, currPos.y+1}); visited[currPos.x][currPos.y+1] = 1; } if (isAccessible(currPos.x-1, currPos.y)) { // 上 s.push({currPos.x-1, currPos.y}); visited[currPos.x-1][currPos.y] = 1; } if (isAccessible(currPos.x+1, currPos.y)) { // 下 s.push({currPos.x+1, currPos.y}); visited[currPos.x+1][currPos.y] = 1; } } // 没有找到路径 cout << "No path found!" << endl; } int main() { // 读入迷宫 cin >> m >> n; for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { cin >> maze[i][j]; visited[i][j] = 0; } } // 求解迷宫问题最短路径 solveMaze(0, 0, m-1, n-1); return 0; } ``` 该代码使用了深度优先搜索来遍历迷宫,并使用栈来存储路径,并在到达终点时输出路径
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值