1. 程式人生 > >棧的應用—求解迷宮問題

棧的應用—求解迷宮問題

問題描述:

給定一個M*N的迷宮圖,求一條從指定入口和出口的迷宮路徑。假設迷宮圖如圖所示:M=N=6的迷宮圖

資料組織:

為了表示迷宮,設定一個數組mg,其中每個元素表示一個方塊的狀態,為0表示方塊是通道,為1表示方塊是障礙物(不可走),為了演算法方便,一般在迷宮的外圍加一條圍牆,例如上圖的迷宮表示為(由於迷宮四周加了一道圍牆,故mg陣列的行數的列數均加上2)

迷宮棧的宣告:

typedef struct     //  定義資料型別
{
    int i,j,di;
} Box;
typedef struct       //   定義棧
{
    Box data[10000];
    int top;
} StType;

具體實現:

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
typedef struct     //  定義資料型別
{
    int i,j,di;
} Box;
typedef struct       //   定義棧
{
    Box data[10000];
    int top;
} StType;
int mg[6][6]=                //  迷宮地圖
{
    {1,1,1,1,1,1},{1,0,0,0,1,1},
    {1,0,1,0,0,1},{1,0,0,0,1,1},
    {1,1,0,0,0,1},{1,1,1,1,1,1}
};
void InitStack(StType *&s)       //  初始化
{
    s=(StType *)malloc(sizeof(StType));
    s->top=-1;
}
bool Push(StType *&s,Box e)    // 出棧
{
    if(s->top==100-1)
        return false;
    s->top++;
    s->data[s->top]=e;
    return true;
}
bool Pop(StType *&s,Box &e)    //  進棧
{
    if(s->top==-1)
        return false;
    e=s->data[s->top];
    s->top--;
    return true;
}
bool Gettop(StType *s,Box &e)   //   得到棧頂元素
{
    if(s->top==-1)
        return false;
    e=s->data[s->top];
    return true;
}
bool Empty(StType *s)      //    判斷是否為空
{
    return (s->top==-1);
}
bool mgpath(int xi,int yi,int xe,int ye,int road)     //  具體實現查詢路徑
{
    Box path[10000],e;
    int i,j,di,i1,j1,k,maproad=0;
    bool Find;
    StType *st;
    InitStack(st);
    e.i=xi,e.j=yi,e.di=-1;
    Push(st,e);
    mg[xi][yi]=-1;
    while(!Empty(st))
    {
        Gettop(st,e);
        i=e.i,j=e.j,di=e.di;
        if(i==xe&&j==ye)
        {
            printf("第%d條迷宮路徑如下:\n",road);
            k=0;
            while(!Empty(st))
            {
                Pop(st,e);
                path[k++]=e;
            }
            while(k>=1)
            {
                k--;
                printf("(%d,%d) ",path[k].i,path[k].j);
                maproad++;
                if((k+2)%5==0)
                    printf("\n");
            }
            printf("\n");
            printf("路徑長度為:%d\n",maproad);
            free(st);
            return true;
        }
        Find=false;
        while(di<4&&!Find)    //     控制查詢的方向
        {
            di++;
            switch(di)
            {
            case 0:
                i1=i-1;
                j1=j;
                break;
            case 1:
                i1=i;
                j1=j+1;
                break;
            case 2:
                i1=i+1;
                j1=j;
                break;
            case 3:
                i1=i;
                j1=j-1;
                break;
            }
            if(mg[i1][j1]==0)Find=true;
        }
        if(Find)
        {
            st->data[st->top].di=di;
            e.i=i1;
            e.j=j1;
            e.di=-1;
            Push(st,e);
            mg[i1][j1]=-1;
        }
        else
        {
            Pop(st,e);
            mg[e.i][e.j]=0;
        }
    }
    free(st);
    return false;
}
int main()     //  主函式
{
    if(!mgpath(1,1,4,4,1))
        printf("該迷宮沒有解\n");
    return 0;
}

思維開拓:

如果你理解了上述的迷宮問題,理解了具體的思路設計,那麼試試這個迷宮吧,下面的這個迷宮要求找出所有的路徑,找到最短路徑並輸出,快來動動腦哦,(趁熱打鐵,熟能生巧)相信這個難不倒聰明的你哦!!!!