严蔚敏数据结构栈之迷宫路径

该代码实现了一个基于栈的算法,用于解决给定迷宫中的路径寻找问题。它首先初始化一个顺序栈,然后通过输入迷宫的行数和列数,以及起点和终点坐标,利用深度优先搜索策略尝试找到从起点到终点的有效路径。当栈不为空时,检查栈顶元素的下一个位置是否可行,若不可行则改变方向,否则将新位置入栈并更新迷宫地图。最后,如果找到终点则打印路径,否则提示无有效路径。
摘要由CSDN通过智能技术生成
#pragma once
#include <stdio.h>
#include <stdlib.h>
#define STACK_INIT_SIZE 100
#define STACKINCREASMENT 10
#define OVERFLOW -2
#define ERROR 0
#define OK 1
#define TRUE 1
#define FALSE 0
typedef int Status;
typedef struct
{
	int x, y;
}PosType;                                  //位置数据结构
typedef struct
{
	int ord;
	PosType seat;
	int di;
}SElemType;                                //Stack ElemType,包含位置,路径长度和下一步走的方 
                                           //向di三个成员
typedef int** MazeType;                    //二维数组指针MazeType
typedef struct                             //Sequence Stack,包含栈底栈底指针以及分配的内存空 
                                           //间三个成员
{
	SElemType* base, * top;
	int stacksize;
}SqStack;
Status Push(SqStack* S, SElemType e)
{
	if (S->top - S->base >= S->stacksize)
	{
		SElemType* base;
		base = (SElemType*)realloc(S->base, (S->stacksize + STACKINCREASMENT) * sizeof(SElemType));
		if (!base) exit(OVERFLOW);
		S->base = base;
		S->top = S->base + S->stacksize;
		S->stacksize += STACKINCREASMENT;
	}
	*S->top = e;
	S->top++;
	return OK;
}
Status Pop(SqStack* S, SElemType* e)
{
	if (S->top == S->base) return ERROR;
	S->top--;
	*e = *S->top;
	return OK;
}
Status InitStack(SqStack* S)
{
	S->base = (SElemType*)malloc(sizeof(SElemType) * STACK_INIT_SIZE);
	if (!S->base) exit(OVERFLOW);
	S->top = S->base;
	S->stacksize = STACK_INIT_SIZE;
	return OK;
}
Status GetTop(SqStack* S, SElemType* e)
{
	if (S->base == S->top) return ERROR;
	*e = *(S->top - 1);
	return OK;
}
Status StackEmpty(SqStack S)
{
	if (S.base == S.top)  return TRUE;
	else return FALSE;
}
Status InitMaze(MazeType* maze, int row, int col)   //以0与1表示墙壁和通路
{
	*maze = (MazeType)malloc(sizeof(int*) * row);
	if (!(*maze)) exit(OVERFLOW);
	for (int i = 0; i < row; i++)
	{
		(*maze)[i] = (int*)malloc(sizeof(int) * col);
		if (!(*maze)[i]) exit(OVERFLOW);
		for (int j = 0; j < col; j++)
		{
			printf("请输入第%d行第%d列的状态\n", i, j);
			scanf("%d", &(*maze)[i][j]);
		}
	}
	return OK;
}
Status GetNext(SElemType* e)            //根据当前的位置以及方向指示获得下一个位置的坐标
{
	int di = e->di;
	switch (di)
	{
	case 1:e->seat.x++; break;
	case 2:e->seat.y--; break;
	case 3:e->seat.x--; break;
	case 4:e->seat.y++; break;
	default:return ERROR;
	}
	e->ord++;
	e->di = 1;
	return OK;
}
void PrintStack(SqStack S)
{
	SElemType* p = S.base;
	while (p != S.top)
	{
		printf("(%d,%d)->", p->seat.x, p->seat.y);
		p++;
	}
}
Status MazePath(MazeType maze, PosType st, PosType en)
{
	SqStack S;
	SElemType* next;
	next = (SElemType*)malloc(sizeof(SElemType));
	if (!next) exit(OVERFLOW);
	InitStack(&S);
	int row, col;
	printf("请输入迷宫行数和列数\n");
	scanf("%d%d", &row, &col);
	InitMaze(&maze, row, col);
	SElemType cur;
	cur.seat.x = st.x;
	cur.seat.y = st.y;
	cur.di = 1;
	cur.ord = 1;
	Push(&S, cur);
	for (int i = 0; i < row; i++)
	{
		for (int j = 0; j < col; j++)
			printf("%d\t", maze[i][j]);
		printf("\n");
	}
	maze[1][1] = 0;
	while (!StackEmpty(S))
	{
		GetTop(&S, next);
		GetNext(next);                            //获得栈顶元素的下一个元素
		if (maze[next->seat.x][next->seat.y] == 0)//如果下一个元素无法通行,更换方向
		{                                         //如果最终四个方向均无法通行,将此地设为0
			Pop(&S, &cur);
			if (cur.di < 4)
			{
				cur.di++;
				Push(&S, cur);
			}
			else
				maze[cur.seat.x][cur.seat.y] = 0;
		}
		else                                      //如果可以通行,不是终点就入栈
		{                                         //是终点就打印栈中从底到顶所有位置
			cur = *next;
			if (cur.seat.x != en.x || cur.seat.y != en.y)
			{
				Push(&S, cur);
				maze[cur.seat.x][cur.seat.y] = 0;
			}
			else
			{
				PrintStack(S);
				printf("(%d,%d)", en.x, en.y);
				return OK;
			}
		}
	}
	printf("无有效路径\n");
	return ERROR;
}
//主函数
int main()
{
	MazeType* maze;
	maze = (MazeType*)malloc(sizeof(MazeType));
	if (!maze) return ERROR;
	PosType st, en;
	printf("请输入起点与终点的坐标值\n");
	scanf("%d%d%d%d", &st.x, &st.y, &en.x, &en.y);
	MazePath(*maze, st, en);
	return 0;
}

输入(严蔚敏数据结构P50):

1 1 8 8
10 10
0 0 0 0 0 0 0 0 0 0
0 1 1 0 1 1 1 0 1 0
0 1 1 0 1 1 1 0 1 0
0 1 1 1 1 0 0 1 1 0
0 1 0 0 0 1 1 1 1 0
0 1 1 1 0 1 1 1 1 0
0 1 0 1 1 1 0 1 1 0
0 1 0 0 0 1 0 0 1 0
0 0 1 1 1 1 1 1 1 0
0 0 0 0 0 0 0 0 0 0

结果:

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值