亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

棧的應用----走出迷宮

標簽:
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
提交
取消