Data structure routine-Maze problem (using stack structure), data structure routine

Source: Internet
Author: User

Data structure routine-Maze problem (using stack structure), data structure routine

This article focuses on the data structure basic series of network courses (3): The application of the 6th Lesson stack in the stack and queue 2-Maze problem.

Example: Find the path from entry to exit

Program Implementation:

# Include <stdio. h> # define MaxSize 100 # define M 8 # define N 8int mg [M + 2] [N + 2] ={{ 1, 1, 1, 1, 1, 1, 1 }, {,}, {,}, {, 0,, ,}, {,}, {,}; typedef struct {int I; // The row number of the current square int j; // The column number int di of the current square; // di is the next azimuth number that can take the adjacent azimuth} 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) // The solution path is: (xi, yi)-> (xe, ye) {int I, j, k, di, find; StType st; // defines the stack st. top =-1; // initialize the stack top pointer st. top ++; // The initial block is pushed to 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) // loop when the stack is not empty {I = st. data [st. top]. i; j = st. data [st. top]. j; di = st. data [st. top]. di; // get the top block of the stack if (I = xe & j = ye) // find the exit, and the output path {printf ("the maze path is as follows: \ 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) // change the printf ("\ n") line after each of the five squares output ");} printf ("\ n"); return (1); // find a path and return 1} find = 0; while (di <4 & find = 0) // find the next square {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 the next adjacent square} if (find = 1) // found the next accessible square {st. data [st. top]. di = di; // modify the di value of the element at the top of the original stack. top ++; // the next block can be taken 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 repeated steps to this square} else // if no path is available, the stack is rolled back {mg [st. data [st. top]. i] [st. data [st. top]. j] = 0; // change the location to another path to go to the square st. top --; // return the block to stack} return (0); // It indicates that no path is available, and 0} int main () {mgpath (, M, n); return 0 ;}

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.