資料結構常式——迷宮問題(用棧結構),資料結構常式

來源:互聯網
上載者:User

資料結構常式——迷宮問題(用棧結構),資料結構常式

  本文針對資料結構基礎系列網路課程(3):棧和隊列中第6課時棧的應用2-迷宮問題。

例:求出從入口到出口的路徑

程式實現:

#include <stdio.h>#define MaxSize 100#define M 8#define N 8int mg[M+2][N+2]={    {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}};typedef struct{    int i;              //當前方塊的行號    int j;              //當前方塊的列號    int di;             //di是下一可走相鄰方位的方位號} Box;typedef struct{    Box data[MaxSize];    int top;            //棧頂指標} StType;               //定義棧類型int mgpath(int xi,int yi,int xe,int ye) //求解路徑為:(xi,yi)->(xe,ye){    int i,j,k,di,find;    StType st;                  //定義棧st    st.top=-1;                  //初始化棧頂指標    st.top++;                   //初始方塊進棧    st.data[st.top].i=xi;    st.data[st.top].j=yi;    st.data[st.top].di=-1;    mg[xi][yi]=-1;    while (st.top>-1)           //棧不空時迴圈    {        i=st.data[st.top].i;        j=st.data[st.top].j;        di=st.data[st.top].di;  //取棧頂方塊        if (i==xe && j==ye)     //找到了出口,輸出路徑        {            printf("迷宮路徑如下:\n");            for (k=0; k<=st.top; k++)            {                printf("\t(%d,%d)",st.data[k].i,st.data[k].j);                if ((k+1)%5==0) //每輸出每5個方塊後換一行                    printf("\n");            }            printf("\n");            return(1);      //找到一條路徑後返回1        }        find=0;        while (di<4 && find==0)     //找下一個可走方塊        {            di++;            switch(di)            {            case 0:                i=st.data[st.top].i-1;                j=st.data[st.top].j;                break;            case 1:                i=st.data[st.top].i;                j=st.data[st.top].j+1;                break;            case 2:                i=st.data[st.top].i+1;                j=st.data[st.top].j;                break;            case 3:                i=st.data[st.top].i,j=st.data[st.top].j-1;                break;            }            if (mg[i][j]==0) find=1;    //找到下一個可走相鄰方塊        }        if (find==1)                    //找到了下一個可走方塊        {            st.data[st.top].di=di;      //修改原棧頂元素的di值            st.top++;                   //下一個可走方塊進棧            st.data[st.top].i=i;            st.data[st.top].j=j;            st.data[st.top].di=-1;            mg[i][j]=-1;                //避免重複走到該方塊        }        else                            //沒有路徑可走,則退棧        {            mg[st.data[st.top].i][st.data[st.top].j]=0;//讓該位置變為其他路徑可走方塊            st.top--;                   //將該方塊退棧        }    }    return(0);                          //表示沒有可走路徑,返回0}int main(){    mgpath(1,1,M,N);    return 0;}

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.