Solving maze problems with stacks

Source: Internet
Author: User

How to find the exit of a maze. First of all to know the maze long what kind, then know the entrance, then is to find the path of the process.

Obviously the main part is how to find access. Let's give an example:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/80/4E/wKiom1c9duXx9BJ0AAAEpBmw1qY091.png "title=" Qq20160519161752.png "alt=" Wkiom1c9duxx9bj0aaaepbmw1qy091.png "/>

In this maze 0 is the wall, 1 is the road. Then we can use a two-dimensional array to represent the maze. Then we need a structure to implement the movement of the position we represent.

struct pos{size_t line;size_t row;};

The structure shows where the maze is now, by recording rows and columns.

Now it's time to start the process of finding a pathway. It's easy to think about finding the next place to go by testing the four or three locations around the current position until the exit is done. But in the course of the temptation, we go to the next position, we must make a mark of the previous position, or our program will always be in two locations to walk around. We are here to prevent it from walking around by placing the previous position.

                if  ( pos.row>0 && maze[pos.line * 10 + pos.row - 1] == &NBSP;1)//left {P.push (pos.line * 10 + pos.row);p os.row--;maze[(pos.line * 10 +  pos.row)] = 2;} else if  (pos.row < 9 && maze[pos.line * 10 +  POS.ROW&NBSP;+&NBSP;1]&NBSP;==&NBSP;1)//Right {P.push (pos.line * 10 + pos.row);p os.row++;maze[( Pos.line * 10 + pos.row)] = 2;} else if  (pos.line > 0 && maze[(pos.line - 1)  * 10 &NBSP;+&NBSP;POS.ROW]&NBSP;==&NBSP;1)//on {P.push (pos.line * 10 + pos.row);p os.line--;maze[( Pos.line * 10 + pos.row)] = 2;} else if  (pos.line < 9 && maze[(Pos.line + 1)  * 10 + pos.row] == 1)//Under {P.push (pos.line * 10 + pos.row );p os.line++;maze[(Pos.line * 10 + pos.row)] = 2;}

This code is on the top and bottom of the four direction of the process of testing, in which we use a stack to record the position we walked, because the labyrinth has a fork, so we need to go back to the wrong time. The stack is selected because of the last-in-first-out feature of the stack. When it comes to backtracking, when we find that the above four temptations are complete without access, we have reached the deepest end of the cul-de-sac and need to backtrack, and the process of backtracking is to take the top element of the stack and then the process of the stack.

maze[(Pos.line * + pos.row)] = 3;pos.line = P.top ()/10;pos.row = P.top ()-Pos.line*10;p.pop ();

The above code is the process of backtracking, plus a decision whether or not to reach the end of the completion of the main part.

if (Pos.line * + Pos.row = =) {Printmaze (maze); return 1;}


This is the process of finding the path, which is the main logical part of our code. The rest is a few caveats.

Read the maze diagram I was through the file pointer, fopen,fgets,fclose to achieve. After we have created a two-dimensional array, we need to convert it to a one-level pointer when we pass the parameter to the function. We do this as a one-dimensional array to handle. Because in memory one-dimensional arrays and two-dimensional arrays are the same, but the expression is not the same way, we this maze by two-dimensional array representation is more intuitive, but with a one-dimensional array can be processed.

The following is the complete code and the results of the run.

Results:

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M02/80/4D/wKioL1c9hLrx1XaAAAAKuzPkpm8071.png "title=" Qq20160519171347.png "alt=" Wkiol1c9hlrx1xaaaaakuzpkpm8071.png "/>

Can see above is the original maze, through the process of our maze, will walk through the fork mark for 3, the correct path marker for 2.


Full code:

#define  _CRT_SECURE_NO_WARNINGS 1#include<iostream> #include <assert.h> #include <stack >using namespace std;struct Pos{size_t line;size_t row;}; Void initmaze (Int *maze) {File *p = fopen ("Maze.txt",  "R");for  (int i  = 0; i < 10; i++) {for  (int j = 0; j <  {int tmp = (int) (FGETC (p))-' 0 ';if  (tmp == 0) maze[i*10+j] = 0;if  ( tmp == 1) maze[i*10+j] = 1;if  (tmp == 1 | |  tmp == 0) j + +;}} Fclose (P);} Void printmaze (Int *maze) {for  (int i = 0; i < 10; i++) { for  (int j = 0; j < 10; j++) {cout << maze[i *  10 + j] <<  " ";} Cout << endl;} Cout << endl;} Int gomaze (Int *maZe,pos start) {assert (Maze);stack<int> p; pos pos;pos.line = start.line;pos.row = start.row;while  (1) {maze[(pos.line  * 10 + pos.row)] = 2;if  (pos.row>0 && maze[pos.line * &NBSP;10&NBSP;+&NBSP;POS.ROW&NBSP;-&NBSP;1]&NBSP;==&NBSP;1)//left {P.push (pos.line * 10 +  Pos.row);p os.row--;maze[(Pos.line * 10 + pos.row)] = 2;} else if  (pos.row < 9 && maze[pos.line * 10 +  POS.ROW&NBSP;+&NBSP;1]&NBSP;==&NBSP;1)//Right {P.push (pos.line * 10 + pos.row);p os.row++;maze[( Pos.line * 10 + pos.row)] = 2;} else if  (pos.line > 0 && maze[(pos.line - 1)  * 10 &NBSP;+&NBSP;POS.ROW]&NBSP;==&NBSP;1)//on {P.push (pos.line * 10 + pos.row);p os.line--;maze[( pos.line * 10 + Pos.row)] = 2;} else if  (pos.line < 9 && maze[(pos.line + 1)  * 10 &NBSP;+&NBSP;POS.ROW]&NBSP;==&NBSP;1)//Under {P.push (pos.line * 10 + pos.row);p os.line++;maze[( Pos.line * 10 + pos.row)] = 2;} else{maze[(Pos.line * 10 + pos.row)] = 3;pos.line = p.top ()  /  10;pos.row = p.top ()  - pos.line*10;p.pop ();} if  (pos.line * 10 + pos.row == 91) {Printmaze (maze); return 1;}}} Void mazetest () {int maze[10][10]; Pos p;p.line = 2;p.row = 0;initmaze ((int *) maze); Printmaze ((int *) maze); Gomaze ((int *) maze,p);} Int main () {mazetest (); return 0;}


This article is from the "Liu Egg Good Study" blog, please be sure to keep this source http://lzd1995.blog.51cto.com/10973198/1775112

Solving maze problems with stacks

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.