为了账号安全,请及时绑定邮箱和手机立即绑定

栈的应用----走出迷宫

标签:
C

走出迷宫

#define _CRT_SECURE_N0_WARNINGS 1//规定 上3下1左2右4#include <iostream>#include <stack>using namespace std;#define ROWSIZE 10#define COLSIZE 10typedef int MazeType[ROWSIZE][COLSIZE];void PrintMaze(MazeType maze){    for (int i = 0; i < ROWSIZE; i++)
    {        for (int j = 0; j < COLSIZE; j++)            printf("%d ", maze[i][j]);        printf("\n");
    }
}typedef struct{
    int row;    int col;
}PosType;typedef struct{
    int ord;//步数
    PosType seat;//位置 
    int di;//1 2 3 4 方向}SelemType;bool Is_Pass(MazeType maze, PosType pos){    return maze[pos.row][pos.col] == 0;
}void FootPrint(MazeType maze, PosType pos){
    maze[pos.row][pos.col] = 8;
}void MarkPrint(MazeType maze, PosType pos){
    maze[pos.row][pos.col] = 4;
}PosType NextPos(PosType pos, int di){    switch (di)
    {    case 1:
        pos.row += 1;        break;    case 2:
        pos.col -= 1;        break;    case 3:
        pos.row -= 1;        break;    case 4:
        pos.col += 1;        break;    default:        break;
    }    return pos;
}void MazePath(MazeType maze,PosType begin, PosType end){
    PosType pos = begin;    int step = 0;
    SelemType e;    stack <SelemType> st;    
    do{        if (Is_Pass(maze, pos))
        {
            e.ord = ++step;
            e.seat = pos;
            e.di = 1;

            FootPrint(maze, pos);            if (pos.row == end.row && pos.col == end.col)                break;
            st.push(e);
            pos = NextPos(pos, 1);
        }        else
        {            if (!st.empty())
            {
                e=st.top(); st.pop();                while (e.di == 4 && !st.empty())
                {
                    MarkPrint(maze, e.seat);
                    e=st.top();
                    st.pop();
                }                if (e.di < 4)
                {
                    e.di++;
                    st.push(e);
                    pos = NextPos(e.seat, e.di);
                }
            }
        }

    } while (!st.empty());

}int main(){
    MazeType maze= {
        { 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 },
    };
    PosType begin = {1,1};
    PosType end = {8,8};

    PrintMaze(maze);
    MazePath(maze,begin,end);    printf("-----------------------------\n");
    PrintMaze(maze);    return 0;
}



运行结果:

webp

image.png



作者:修夏之夏i
链接:https://www.jianshu.com/p/5181a491fc0d


点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号

举报

0/150
提交
取消