关于迷宫问题 利用栈实现

本文探讨了如何使用C++和栈数据结构解决迷宫问题。通过深度优先搜索(DFS)策略,利用栈进行路径探索,找到从起点到终点的解。详细介绍了算法步骤和代码实现,帮助读者理解栈在解决这类问题中的关键作用。

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

#include <stdio.h>
#include<stdlib.h>
#define M 10
#define N 10
#define STACK_INIT_SIZE 100
#define STACK_INC 10
int flag=0;
typedef struct{
	int x;
	int y;
}Position;

typedef struct{
	Position pos;
	int id;
	int dir;
}ElemType;

typedef struct{
	ElemType *base;
	ElemType *top;
	int StackSize;
}SqStack;

void InitStack(SqStack &S){
	S.base=(ElemType *)malloc(STACK_INIT_SIZE*sizeof(ElemType));
	S.top=S.base;
	S.StackSize=STACK_INIT_SIZE;
}

void push(SqStack &S,ElemType e){
	if(S.top-S.base>=S.StackSize){
		S.base=(ElemType *)realloc(S.base,(STACK_INIT_SIZE+STACK_INC)*sizeof(ElemType));
		S.top=S.base+S.StackSize;
		S.StackSize+=STACK_INC;
	}
	*S.top=e;
	S.top++;
}

void pop(SqStack &S,ElemType &e){
	S.top--;
	e.dir=(*(S.top)).dir;
	e.id=(*(S.top)).id;
	e.pos.x=(*(S.top)).pos.x;
	e.pos.y=(*(S.top)).pos.y;
}

int StackEmpty(SqStack	S){
	if(S.base==S.top)return 1;
	return 0;
}


void iniarr(int (*p)[N]){
    int i,j;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值