栈模拟迷宫

这段代码展示了如何使用栈来模拟迷宫路径寻找,通过入栈、出栈操作,判断当前位置是否可以通过并找到从起点到终点的路径。程序中定义了栈的结构,初始化、入栈、出栈等操作,以及路径搜索的实现。
摘要由CSDN通过智能技术生成
#include <stdio.h>
#include <stdlib.h>

#define STACK_SIZE 1
#define STACK_ADD 1
#define FLASE 0
#define TRUE 1
#define N 10
#define M 10

typedef int Elemtype;
typedef int Status;

int curstep = 1;  //探索的步数
int map[M][N];

typedef struct {
    int x;
    int y;
}PosType;  //坐标定义

typedef struct {
    int ord;   //通道块在路径上的“序号”
    PosType seat;   //通道块在迷宫中的“坐标位置”
    int di;  //从此通道块走向下一通道块的“方向”,上下左右
}SElemType;  //栈的元素类型

typedef struct {
    SElemType *base;  //栈的基地址
    SElemType *top;   //栈顶指针
    int stacksize;  //栈大小
}sql_stack;  //栈定义

sql_stack L;

Status InitStack(sql_stack &L)  //初始化栈
{
    L.base = (SElemType *)malloc(STACK_ADD * sizeof(SElemType));
    if (!L.base)
        exit(FLASE);
    L.top = L.base;
    L.stacksize = STACK_SIZE;
    return TRUE;
}

Status Push(sql_stack &L, SElemType e)  //入栈
{
    if (L.top - L.base >= L.stacksize)
    {
        L.base = (SElemType *)realloc(L.base,(L.stacksize + STACK_ADD)*sizeof(SElemType));
        if (!L.base)
            exit(FLASE);
        L.top = L.base + L.stacksize;
        L.stacksize += STACK_ADD;
    }
    *L.top++ = e;
    return TRUE;
}

Status Pop(sql_stack &L, SElemType &e)  //出栈
{
    if (L.top == L.base)
        return FLASE;
    e = *--L.top;
    return TRUE;
}

Status StackEmpty(sql_stack L)  //判栈空
{
    return (L.top == L.base);
}

void MarkPrint(PosType b)  //标记
{
    map[b.x][b.y] = 1;
}

Status Pass(PosType k)  //能否走
{
    if (0 == map[k.x][k.y])
        return TRUE;
    else
        return FLASE;
}

PosType NextPass(PosType c, int di)  //下一步
{
    PosType direc[4] = {{0,1},{1,0},{0,-1},{-1,0}};
    c.x += direc[di].x;
    c.y += direc[di].y;
    return c;
}

void FootPrint(PosType o)  //走过的路
{
    map[o.x][o.y] = curstep;
}

Status Path(PosType begin, PosType end)  //判定
{
    PosType curpos;
    SElemType e;
    InitStack(L);
    curpos = begin;
    do
    {
        if (Pass(curpos))
        {
            FootPrint(curpos);
            e.ord = curstep;
            e.seat.x = curpos.x;
            e.seat.y = curpos.y;
            e.di = 0;
            Push(L,e);
            curstep++;
            if (curpos.x == end.x && curpos.y == end.y)
            {
                return TRUE;
            }
            curpos = NextPass(curpos,e.di);
        }
        else
        {
            if (curpos.x == end.x && curpos.y == end.y)
                break;
            if (!StackEmpty(L))
            {
                Pop(L,e);
                curstep--;
                while (e.di == 3 && !StackEmpty(L))
                {
                    MarkPrint(e.seat);
                    Pop(L,e);
                    curstep--;
                }
                if (e.di < 3)
                {
                    e.di++;
                    Push(L,e);
                    curstep++;
                    curpos = NextPass(e.seat,e.di);
                }
            }
        }
    }while (!StackEmpty(L));
    return FLASE;
}

int main()
{
    PosType begin,end;
	SElemType e;
	int i;
	int x=0,y=0,count=0,m,n,mapresult[100];
	printf("\t迷宫的行数m=");
	scanf("%d",&m);
	printf("\t迷宫的列数n=");
	scanf("%d",&n);
	printf("\t迷宫地图初始化:0代表通路,1代表障碍,用空格分开\n");
	for(i=0;i<m;i++)
	{
	   printf("\t ");
	   for(int j=0;j<n;j++)
		 scanf("%d",&map[i][j]);
	}
	printf("\t输入起点的横坐标,纵坐标[逗号隔开]\n");
	scanf("%d,%d",&begin.x,&begin.y);
	printf("\t输入终点的横坐标,纵坐标[逗号隔开]\n");
	scanf("%d,%d",&end.x,&end.y);
	if(Path(begin,end))
	{
		printf("\t此迷宫从入口到出口的路径如下:\n");
		while(!StackEmpty(L))
		{
			Pop(L,e);
			mapresult[count]=e.seat.x;
			mapresult[++count]=e.seat.y;
			count++;
		}
		printf("\t");
		for(int i=count-1;i>=0;i=i-2)
		{
		    printf("[%d,%d] ",mapresult[i-1],mapresult[i]);
		}
		printf("\n");
	}
	else
	{
		printf("\tNo Found!\n");
	}
	return 0;
}

以上代码是依据严蔚敏书第51页上的思路写的,并查看了他人的代码后总结的。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值