Let's take a look at this maze----phalanx (or matrix):
650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M01/7E/E9/wKiom1cMngag6p87AAAffRONdMg639.png "title=" Ho6el69h]pj$@y}5hys_6z4.png "alt=" Wkiom1cmngag6p87aaaffrondmg639.png "/>
The labyrinth entrance is the coordinate (2,0) position, the exit is (9,3). We assume that the 0 represents the pathway and that 1 represents a non-pass.
Now we need to find out which way is the pathway. Our idea is the use of stacks,"backtracking". What does backtracking mean??? Start with the starting point and check whether it is a pathway (i.e. whether it is a number 0) or not. That is, 0 pass, press the stack, this position element into 2, the advantage of doing so is to clear the path. Then go down and judge up and down. Until we find the end point (ordinate in the last line of the matrix).
Let's take a look at the code I implemented for the maze problem:
#include <stack> #include <assert.h> #define &NBSP;N&NBSP;10&NBSP;&NBSP;&NBSP;&NBSP;//the Maze 10*10.struct pos //defines a structure that is used to represent coordinates. { int _row; int _col; pos (int row,int col) :_row (Row) , _col (COL) {}};template<class T>bool Searchmazepath (int* a, int n, pos entry, stack<t>& paths) //Find out if the maze has access. { assert (a); paths.push (entry); while (!paths.empty ()) { pos cur = paths.top (); a[cur._row*n + cur._col] = 2; if (cur . _row == n - 1) { return true; } //on Pos tmp = cur; --tmp._row; if (a[tmp._row*n + tmp._col] == 0) { Paths.push (TMP); continue; } //under &Nbsp; tmp = cur; ++tmp._row; if (a[tmp._row*n + tmp._col] == 0) { paths.push (TMP); continue; } //left tmp = cur; --tmp._col; if (a[tmp. _row*n + tmp._col] == 0) { paths.push (TMP); continue; } //Right tmp = cur; ++tmp._col; if (a[tmp._row*n + tmp._col] == 0) { paths.push (TMP); continue; } paths.pop (); //if the upper and lower left or right is not through, then backtracking. } return&nbSp;false;} Void getmaze (int* a, int n) //read to maze pattern { ASSERT (a); file* fout = fopen ("MazeMap.txt", "R"); assert (fout); //If the file fails to open, subsequent work cannot be completed, so use assert defensive programming. for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { while (True) { &NBSP;&NBSP;&NBSP;&NBSP;INT&NBSP;MEN&NBSP;=&NBSP;FGETC (fout); //Read characters if (men == ' 0 ' | | men == ' 1 ') //is 0 or 1 is converted into a digital to a two-dimensional matrix stored. { a[ i*n + j] = men -' 0 '; break; } } } } }void printmaze (int* a, int n) &NBSP;&Nbsp; //The maze to print out { for (int i = 0; i < n ; i++) { for (int j = 0; j < n;j++ ) { cout << a[i*n + j] << " "; } cout << endl; } cout < < endl;} Void test () {&NBSP;&NBSP;&NBSP;&NBSP;INT&NBSP;A[N][N]&NBSP;=&NBSP;{};&NBSP;&NBSP;&NBSP;&NBSP;POS&NBSP;SP (2 , 0); //Entry coordinate getmaze ((int*) a, n); printmaze ((int*) a, n); stack<pos> Paths; searchmazepath ((int*) a, n, sp, paths); //Two-dimensional arrays are actually stored as one-dimensional arrays, forcing two-dimensional arrays into one-dimensional array parameters. printmaze ((int*) a, n);} Int main () { test (); system ("Pause"); return 0;}
Sometimes, for the maze problem, we also need to find the optimal solution of multiple paths (shortest path). That's when we can use a stack of stacked frames, a layer of laminated stack to achieve the characteristics.
This article is from the "C language 100-200 Prime" blog, please be sure to keep this source http://10740184.blog.51cto.com/10730184/1763002
"C + +" uses stack stack to solve maze problems