栈实现-迷宫问题

本文介绍了一种使用栈和队列解决迷宫问题的方法,详细阐述了迷宫地图的表示方式和算法流程,通过实例展示了如何利用数据结构解决实际问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

给定一个迷宫。如何走出去?最近有看看栈和队列

就想用栈和队列实现,写了半天,终于写出来了。。。

// 0 代表此路可走 1 代表此路不通
// 只能走四个方向,即上下左右
//周围的 111... 代表围墙。。
#include<stdio.h>
#include<stdlib.h>
#define mac 100		//定义栈的大小限制
typedef struct
{
	short int row;		//行
	short int col;		//列
	short int dir;		//方向
}element;

element stack[mac];
typedef struct
{
	short int vert;			//垂直方向
	short int horiz;		//水平方向
}offset;

offset move[4] = {{-1,0},{0,1},{1,0},{0,-1}};		//4个方向的偏移量
int top = -1;
int maze[10][10] =
{
	{1,1,1,1,1,1,1,1,1,1},
	{1,0,1,0,0,0,1,1,0,1},
	{1,0,0,1,1,0,0,1,1,1},
	{1,1,0,0,0,1,1,0,0,1},
	{1,0,0,0,0,0,0,0,0,1},
	{1,1,0,0,0,0,1,1,1,1},
	{1,1,1,0,0,0,0,0,0,1},
	{1,1,1,1,0,0,0,0,1,1},
	{1,1,1,0,0,0,0,0,0,1},
	{1,1,1,1,1,1,1,1,1,1}
};

int mark[10][10];
#define enter_row 1
#define enter_col 1

#define exit_row 8
#define exit_col 8

void path();
void push(int *top,element a)
{
	++(*top);
	if(*top <= mac - 1)
		stack[*top] = a;
	else
	{
		printf("over flow!!!\n");
		exit(1);
	}
}
element pop(int *top)
{
	if(*top >= 0)
		return (stack[(*top)--]);
	else
	{
		printf("stack overflow!!!\n");
		exit(1);
	}
}
element gettop(int top)
{
	if(top>=0)return (stack[top]);
	else
	{
		printf("stack undefined.\n");
		exit(1);
	}
}
void path()
{
	int i,j;
	int row,col,next_row,next_col;
	int dir,found = 0;				//found 标记参数 0代表未访问 1代表访问
	element position;

	for(i=0;i<10;i++)
		for(j=0;j<10;j++)
		mark[i][j] = 0;
	mark[1][1] = 1;					//初始化标记地图

	top = 0;
	stack[0].row = enter_row;		//将入口推进栈里面
	stack[0].col = enter_col;
	stack[0].dir = 0;

	while(top>-1&&!found){
		position = gettop(top);		//获取栈顶元素
		row = position.row;
		col = position.col;
		dir = position.dir;

		while(dir<4&&!found){		//没有发现出口
			next_row = row + move[dir].vert;
			next_col = col + move[dir].horiz;		//探寻下一个方向,首先是上面

			if(next_row == exit_row && next_col == exit_col)
				found = 1;							//找到出口

			//如果找到下一个可以走的地方将下一个地方作为新的入口
			else if(!maze[next_row][next_col] && !mark[next_row][next_col]){
				mark[next_row][next_col] = 1;
				position.row = next_row;  position.col = next_col;
				position.dir = ++dir;
				push(&top,position);
				row = next_row; col = next_col; dir = 0;

			}

			//找不到,重新试探方向
			else ++dir;
		}

		//再找不到说明死路,退栈
		if(!found)
			position = pop(&top);
	}

	//找到的话就输出寻找的痕迹 1 为探索过
	if(found == 1){
		mark[8][8]=1;
		printf("探索过的地方为:\n");
		for(i=0;i<10;i++)
		{
			for(j=0;j<10;j++)
				printf("%-2d ",mark[i][j]);
			printf("\n");
		}
	}


	if(found == 1)
	{
		printf("     行走的路径为:\n");
		printf("  步数   行号  列号.\n");
		for(i=0;i<top;i++)
			printf("%5d %5d %5d\n",i+1,stack[i].row,stack[i].col);
		printf("%5d %5d %5d\n",i+1,exit_row,exit_col);
	}

	else
		printf("the maze don't have a path.\n");
}
int main()
{
	path();
	return 0;
}


程序在VC++ 6下顺利编译通过。 一、 实验目的: (1) 熟练掌握链的基本操作及应用。 (2) 利用链表作为的存储结构,设计实现一个求解迷宫的非递归程序。 二、实验内容: 【问题描述】 以一个m×n的长方阵表示迷宫,0和1分别表示迷宫中的通路和障碍。设计一个程序,对信任意设定的迷宫,求出一条从入口到出口的通路,或得出没有通路的结论。 【基本要求】 首先实现一个链表作存储结构的类型,然后编写一个求解迷宫的非递归程序。求得的通路以三元组(i,j,d)的形式输出,其中:(i,j)指示迷宫中的一个坐标,d表示走到下一坐标的方向。如:对于下列数据的迷宫,输出的一条通路为:(1,1,1),(1,2,2),(2,2,2),(3,2,3),(3,1,2),……。 【测试数据】 迷宫的测试数据如下:左上角(1,1)为入口,右下角(8,9)为出口。 1 2 3 4 5 6 7 8 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 以方阵形式输出迷宫及其通路。 输出: 请输入迷宫的长和宽:5 5 请输入迷宫内容: 0 1 1 0 0 0 0 1 1 0 1 0 0 1 1 1 0 0 1 0 1 1 0 0 0 迷宫的路径为 括号内的内容分别表示为(行坐标,列坐标,数字化方向,方向) (1,1,1,↓) (2,1,2,→) (2,2,1,↓) (3,2,1,↓) (4,2,2,→) (4,3,1,↓) (5,3,2,→) (5,4,2,→) (5,5,0,) 迷宫路径探索成功!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值