简单迷宫,字符化输出

简单迷宫,字符化输出

效果图

#include <stdio.h>
#include <stdlib.h>
#define M 8
#define N 8
#define TRUE 1
#define FALSE 0

typedef int bool;
typedef struct{		// 方位数据结构
    int incX, incY;
} Direction;

typedef struct{
    int x, y;   // 当前位置坐标
    int di;     // 当前方位
} Box, ElemType;          // 栈中元素

typedef struct StackNode{   // 栈
    ElemType data;
    struct StackNode *next;
} StackNode, *LinkStack;
bool Push(LinkStack S, ElemType e){
    LinkStack p=(LinkStack)malloc(sizeof(StackNode));
    p->data=e;
    p->next=S->next;
    S->next=p;
    return 1;
}
bool Pop(LinkStack S, ElemType *e){
    if(S==NULL) return -1;
    *e=S->next->data;
    LinkStack p=S->next;
    S->next=S->next->next;
    free(p);
    return 1;
}

void Traverse(LinkStack S, int graph[M+2][N+2]){
    int x, y;
    ElemType p; 
    while(S->next!=NULL){
        Pop(S, &p);        
        graph[p.x][p.y]=-2;	// 标记路径
    }
    for(x=0; x<M+2; x++){	// 字符输出
        for(y=0; y<N+2; y++){
            if(graph[x][y]==-2)
                printf("%2c", 'O');
            else if(graph[x][y]==1)
                printf("%2c", '-');
            else printf("%2c", ' ');
        }
        printf("\n");
    }
}

bool findPath(int maze[M+2][N+2], Direction direct[], LinkStack S){
    Box temp;
    int x, y, di;   // 迷宫格子当前处理单元的横纵坐标和方向
    int line, col;  // 迷宫数组下一单元的行坐标和列坐标
    maze[1][1]=-1;
    temp.x=1; temp.y=1; temp.di=-1;
    Push(S, temp);
    while(S->next!=NULL){
        Pop(S, &temp);
        x=temp.x; y=temp.y; di=temp.di+1;
        while(di<4){    // 方向未尝试完
            // printf("%d %d %d\n", x, y, di);
            line=x+direct[di].incX;
            col=y+direct[di].incY;
            if(maze[line][col]==0){
                temp.x=x; temp.y=y; temp.di=di;
                Push(S, temp);
                x=line; y=col; maze[line][col]=-1;
                if(x==M&&y==N) return TRUE;     // 发现出口 
                else di=0;
            }
            else di++;
        }
    }
    return FALSE;
}


int main(){
    int maze[M+2][N+2] = {	// 1-障碍
        { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
        { 1, 0, 0, 1, 0, 0, 0, 1, 0, 1 },
        { 1, 0, 0, 1, 0, 0, 0, 1, 0, 1 },
        { 1, 0, 0, 0, 0, 1, 1, 0, 0, 1 },
        { 1, 0, 1, 1, 1, 0, 0, 0, 0, 1 },
        { 1, 0, 0, 0, 1, 0, 0, 0, 0, 1 },
        { 1, 0, 1, 0, 0, 0, 1, 0, 0, 1 },
        { 1, 0, 1, 1, 1, 0, 1, 1, 0, 1 },
        { 1, 1, 0, 0, 0, 0, 0, 0, 0, 1 },
        { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }
    };
    Direction direct[4] = {	// 四个方向
        {  0, 1 },	// right
        {  1, 0 },	// down
        {  0,-1 },	// left
        { -1, 0 }	// up
    };
    LinkStack S=(LinkStack)malloc(sizeof(StackNode));
    S->next=NULL;
    int rst=findPath(maze, direct, S);	// 寻径
    Traverse(S, maze);

	free(S);
    return 0;
}

参照:B栈懒猫老师

https://www.bilibili.com/video/BV1oE41177wk

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值