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 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; The line number of the current squareInt J; Column number of the current blockint di; DI is the bearing number of the next adjacent azimuth} Box;typedef struct{BOX Data[maxsize];int top; Stack top pointer} sttype; Defining the Stack typeint mgpath (int xi,int yi,int xe,int ye)//Solution path: (Xi,yi), (xe,ye) {int I,j,k,di,find;SttypeSt; Define Stack St    St. Top=-1; Initialize 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 blockif (i==xe && j==ye)//found exit, Output path {printf ("The maze path is as follows: \ n");for (k=0; k<=st.top; k++){printf ("\ t (%d,%d)",St. DataK. I,St. DataK. J);if ((k +1)%5==0)//per output per5Block after a row of printf ("\ n");} printf ("\ n");Return1); Returns 1 when a path is found} find=0;while (di<4&& find==0)//Find the next available block {di++;Switch (DI) {case0: i=St. Data[St. Top]. I-1;j=St. Data[St. Top]. J;                 Break;Case1: i=St. Data[St. Top]. I;j=St. Data[St. Top]. J+1;                 Break;Case2: i=St. Data[St. Top]. I+1;j=St. Data[St. Top]. J;                 Break;Case3: i=St. Data[St. Top]. I, j=St. Data[St. Top]. J-1;                 Break;} if (mg[i][j]==0) find=1; To find the next adjacent block.} if (find==1)//found the next removable block {St. Data[St. Top]. Di=di; Modifies the Di value of the top element of the original stack            St. Top++; Next walking block 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 repeating to the block.} else//does not have a path to go, then the fallback stack {mg[St. Data[St. Top]. I][St. Data[St. Top]. J]=0;//Make the location a different path to go block            St. Top--; The block is retired from the stack}} return (0); Indicates no path to go, return 0}int Main () {Mgpath (1,1, M,n);Return0;}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Data structure Routines-maze problem (with stack structure)

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.