Data structure Routines-maze problem (with stack structure)

Source: Internet
Author: User

This paper is aimed at the basic series of data Structure network course (3): Stack and queue of the 6th lesson stack Application 2-maze problem.

Example: Finding the path from the inlet to the exit

Program implementation:

#include <stdio.h> #define MAXSIZE #define M 8 #define N 8 int 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;              The line number of the current block Int J;             The column number of the current block int di;
Di is the next azimuth number of the adjacent position} Box;
    typedef struct {Box data[maxsize];            int top;               Stack top pointer} sttype;
    Defines the stack type int mgpath (int xi,int yi,int xe,int ye)//Solution path: (Xi,yi)--(Xe,ye) {int i,j,k,di,find;                  Sttype St;                  Define Stack St St.top=-1;                   Initializes the top pointer of the stack st.top++;
    Initial block into the stack 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)//stack is not empty when looping {i=st.data[st.top].i; J=ST.DATA[ST.TOP].J;  Di=st.data[st.top].di;
            Take stack top box if (i==xe && j==ye)//Find exit, Output path {printf ("Maze path: \ 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)//output every 5 blocks after a line of printf ("\ n");
            } printf ("\ n");      return (1);
        Returns 1} find=0 after finding a path;
            while (di<4 && find==0)//Find the next removable block {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;      Find Next adjacent block} if (find==1)//Find the next removable block {st.data[st.top].di=di;                   Modifies the DI value st.top++ of the top element of the original stack;
            Next can walk square into the stack st.data[st.top].i=i;
            St.data[st.top].j=j;
            St.data[st.top].di=-1;                Mg[i][j]=-1; Avoid repeatedly walking to the block} else//no path to go, then stack {mg[st.data[st.top].i][st.                   data[st.top].j]=0;//let the location become another path can walk block st.top--;                          Return The Block}} return (0);
    Indicates that there is no path to go, return 0} int main () {Mgpath (1,1,m,n);
return 0;
 }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.