堆栈实现的迷宫寻路

这篇博客探讨了如何利用数据结构中的堆栈来解决迷宫寻路的经典问题,通过C语言实现代码详细解析了算法过程。
摘要由CSDN通过智能技术生成

    这是数据结构中堆栈的一个习题,代码如下

/*迷宫搜索程序
 * 用数组来表示迷宫,1为墙,0为可走的路。在寻找出口时,用2来标记走过的路
 * 为了避免边界检查,所以在迷宫外加了一圈1(也就是墙),默认入口为maze[1][1],出口为maze[EXIT_ROW][EXIT_COL]
 * 有8个移动方向,做成了数组,方便调用
 */
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define M 6	//迷宫的规模,如果修改此处,也要相应修改迷宫数组
#define P 8
#define EXIT_ROW 4	//出口位置
#define EXIT_COL 6
#define MAX_STACK_SIZE 200 //栈的最大长度

typedef struct {
	short int vert;
	short int horiz;
}offsets;
typedef struct {
	short int row;
	short int col;
}element;


//移动方向的数组
offsets move[8] = {
	-1, 0,
	-1, 1,
	0, 1,
	1, 1,
	1, 0,
	1, -1,
	0, -1,
	-1, -1
};
//用于储存的栈
element stack[MAX_STACK_SIZE];
int top;

//对于stack的delete,add操作
element delete (int *top)
{
	(*top)--;
	element new = stack[*top];
	return new;
}

void add (int *top, element position)
{
	stack[*top] = position;
	(*top)++;
}	


//表示迷宫的数组,1代表墙
static int maze[M][P] = {
	{1, 1, 1, 1, 1, 1, 1, 1},
	{1, 0, 0, 0, 0, 0, 0, 1},
	{1, 1, 1, 1, 0, 1, 0, 1},
	{1, 1, 1, 1, 0, 1, 0, 1},
	{1, 1, 1, 1, 1, 1, 0, 1},
	{1, 1, 1, 1, 1, 1, 1, 1},
};

//打印迷宫
void printmaze (void)
{
	int i, j;
	printf ("*********************************\n");
	for (i = 0; i < M; i++)
	{
		for (j = 0; j < P; j++)
			printf ("%4d",maze[i][j]);
		printf ("\n");
	}
	printf ("*********************************\n");
}

//迷宫寻路函数
void path (void)
{
	int i, row, col, next_row, next_col, dir, found = false;
	element position;
	top = 1; //top总是为下一个栈的下标
	stack[0].row = 1;
	stack[0].col = 1;
	while (top > -1 && !found)
	{
		position = delete (&top); //弹出一个元素
		row = position.row;
		col = position.col;
		dir = 0;
		maze[row][col] = 2;	//标记为走过
		while (dir < 8 && !found)
		{
			next_row = row + move[dir].vert;
			next_col = col + move[dir].horiz;
			if (next_row == EXIT_ROW && next_col == EXIT_COL) //如果找到了出口,标记走过并压栈
			{
				maze[next_row][next_col] = 2;
				position.row = next_row;
				position.col = next_col;
				add (&top,position);
				found = true;
			}
			else if (maze[next_row][next_col] == 0 ) //如果下一步不是墙而且没走过,压栈
			{
				position.row = next_row; 
				position.col = next_col;
				dir++;
				add (&top,position); 
				//printf ("top = %d\n",top);
			}
			else 	//否则尝试下一个方向
				++dir;
		}
		printmaze();//打印迷宫
	}

	if (found)	//如果找到输出找到提示
	{
		printf ("The maze has a path\n");
//		for (i = 0; i < top; i++)
//		{
//			printf ("(%d,%d)\n", stack[i].row, stack[i].col); 
//		}
	}
	else	//否则输出未找到
		printf ("The maze does not have a path\n");
}

//主函数中调用path
int main (void )
{
	path();
	return 0;
}
在写出栈和进栈函数时,搞错了好几次。从栈弹出一个元素,top需要减一,这样才会被后来压入的给覆盖掉。

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值