栈实现迷宫求解问题

#include <stdio.h>
#include <stdlib.h>

// 栈 后进先出

typedef struct{
    int i;
    int j;
    int Type;
}PosType, MazeType[100];
typedef struct{
    int ord; // 通道块在路径上的序号
    PosType seat;
    int di;
}SElemType;


typedef int Status;   // 函数返回数据类型

#define TRUE 1
#define OK 1
#define FALSE 0
#define ERROR -1
#define OVERFLOW -2

#define STARKINIT 100
#define INCREMENT 10

#define NO -1
#define GO 0
#define EVER -2
#define OVER 9999

typedef struct{
    SElemType *base;
    SElemType *top;
    int stackSize;
}SqStack;

Status InitStact(SqStack *S){
    S->base = (SElemType *)malloc(STARKINIT * sizeof(SElemType));
    if(!S->base) exit(OVERFLOW);
    S->top = S->base;
    S->stackSize = STARKINIT;
    return OK;
}

Status DestroyStack(SqStack *S){
    free(S);
    return OK;
}

Status ClearStack(SqStack *S){
    S->top = S->base;
    return OK;
}

Status StackEmpty(SqStack S){
    if(S.top == S.base) return TRUE;
    return FALSE;
}

int StackLength(SqStack S){
    return S.top-S.base;
}

Status GetTop(SqStack S, SElemType *e){
    if(S.top == S.base) return ERROR;
    *e = *(S.top-1);
    return OK;
}

Status Push(SqStack *S, SElemType e){
    if(S->top - S->base >= S->stackSize){
        S->base = (SElemType *)realloc(S->base,(S->stackSize + INCREMENT)* sizeof(SElemType));
        if(!S->base) exit(OVERFLOW);
        S->top = S->base + S->stackSize;
        S->stackSize += INCREMENT;
    }
    *S->top++ = e;
    return OK;
}

Status Pop(SqStack *S, SElemType *e){
    if(S->top == S->base) return ERROR;
    *e = * --S->top;
    return OK;
}

/*
    迷宫开始
*/


PosType InitPos(int i, int j, int Type){
    PosType p;
    p.i = i;
    p.j = j;
    p.Type = Type;
    return p;
}

// 初始化迷宫为书上的迷宫
void InitMaze(MazeType m){
    for(int i=0; i<10; i++)
        for(int j=0; j<10; j++){
            m[i*10+j] = InitPos(i,j,GO);
            if(i == 0 || i==9 || j==0 || j==9) m[i*10+j].Type = NO;
            else if((j==1 && i==8) ||
                    (j==2 && (i==4 || i==6 || i==7)) ||
                    (j==3 && (i<3 || i==4 || i==7)) ||
                    (j==4 && (i==4 || i==5 || i==7)) ||
                    (j==5 && i==3) ||
                    (j==6 && (i==3 || i==6 || i==7)) ||
                    (j==7 && (i<3 || i==7)))
                m[i*10+j].Type = NO;
        }
}

// 是否能够通过
Status Pass(PosType p){
    if(p.Type == GO) return TRUE;
    return FALSE;
}

// 经过该路径
void FootPrint(PosType *p){
    p->Type = OVER;
}

// 该位置不能通过
void MarkPrint(PosType *p){
    p->Type = EVER;
}

// 向下走一步,1234对应东南西北
PosType NextPos(PosType p, int step){
    if(step == 1){
        p.j++;
        return p;
    }else if(step == 2){
        p.i++;
        return p;
    }else if(step == 3){
        p.j--;
        return p;
    }else{
        p.i--;
        return p;
    }
}

// 若迷宫maze中存在从入口到出口end的通道,则求得一条存放在栈中
// 成功返回TRUE,否则返回FALSE
Status MazePath(MazeType maze, PosType start, PosType end){
    SqStack S;
    InitStact(&S);
    PosType curpos = start;
    SElemType e;
    int curstep = 1;

    do{
        if(Pass(curpos)){
            FootPrint(&maze[curpos.i*10 + curpos.j]);
            e.di = 1;
            e.seat = curpos;
            e.ord = curstep;
            Push(&S,e);
            if(curpos.i==end.i && curpos.j==end.j) return TRUE;
            curpos = NextPos(curpos,1);
            curpos = maze[curpos.i*10 + curpos.j];
            curstep++;
        }else{
            if(!StackEmpty(S)){
                Pop(&S,&e);
                while(e.di==4 && !StackEmpty(S)){
                    MarkPrint(&maze[e.seat.i*10 + e.seat.j]);
                    Pop(&S,&e);
                }
                if(e.di < 4){
                    e.di++;
                    Push(&S,e);
                    curpos = NextPos(e.seat,e.di);
                    curpos = maze[curpos.i*10 + curpos.j];
                }
            }
        }
    }while(!StackEmpty(S));
    return FALSE;
}


int main()
{
    MazeType m;
    InitMaze(m);
        for(int i=0; i<10; i++){
            for(int j=0; j<10; j++){
                printf("%d\t",m[i*10+j].Type);
            }
            printf("\n");
        }
    if(MazePath(m,m[10+1],m[8*10+8])){
        printf("\n通过\n");
        for(int i=0; i<10; i++){
            for(int j=0; j<10; j++){
                printf("%d\t",m[i*10+j].Type);
            }
            printf("\n");
        }
    }else{
        printf("失败");
    }

    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值