"Maze problem (Stack)"
"Stack" is a simple data structure, its main feature is "advanced after-out", that is, the first push into the stack of data need to be the last stack. The equivalent of a stack is a cup, first put in the stack of things, can only be taken out finally. The following is the "stack" of the characteristics of the image of the figure expressed.
650) this.width=650; "Width=" 570 "height=" 263 "title=" Untitled. png "style=" WIDTH:361PX;HEIGHT:167PX; "src="/HTTP/ S4.51cto.com/wyfs02/m01/7e/e2/wkiom1clirec60leaaam9haxaou999.png "alt=" Wkiom1clirec60leaaam9haxaou999.png "/>
What we're going to talk about is the stack-based maze problem, how can we find the channel in the maze given a maze? If the size of the maze is larger, how can we achieve it? We can clearly understand the need to use a two-dimensional array to save the maze, but when the size of the maze is large, or when we want to change the design of the maze frequently, it is obviously unreasonable to use a two-dimensional array, the program changes will be more troublesome.
Here I use "file" way to save the maze, the program only to implement the operation to read the file, so it is easy to later maintenance of the program. Because of the dynamic size of a given maze, the implementation of the program is still more troublesome, the scope of the maze is given directly. Use "1" to indicate not to pass, "0" means to pass. Here is the design of the maze:
650) this.width=650; "width=" 389 "height=" 227 "title=" Untitled. png "style=" WIDTH:356PX;HEIGHT:189PX; "src="/HTTP/ S3.51cto.com/wyfs02/m02/7e/e2/wkiom1clkc3yqyotaaakbbxam2k534.png "alt=" Wkiom1clkc3yqyotaaakbbxam2k534.png "/>
For the characteristics of the maze, the main method is to first put the entry point of the maze into the stack, in the point of the upper, lower, left and right direction of the node to judge, see if there is able to pass through the node ("0"), if not, there is no way to pass through the maze, if there is, the node is also pressed into the stack, Loop this way until you find a point where there is no way forward, it is time to "backtrack", that is, back to the point of the past. In order to determine whether there is no past and can pass through the node, if there is, again, the pressure stack operation, until out of the maze, if the entry point back to the maze, there is no path in the maze.
According to the above ideas, write the following code:
Memory.h file #pragma once#define max 10#include <stack> #include <assert.h> struct pos //the position coordinates of a marker point using a static array { int _row; int _col;}; Void getmaze (int * arr, int n) //get the maze from the file { assert (arr); file* open = fopen ("F:\\keshe\\ Maze problem (Stack) \\MazeMap.txt", "R"); assert (open); //determine if Open file is successful for (int i = 0; i < max; i++) { for (int j = 0; j < max; ) { &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;CHAR&NBSP;CH&NBSP;=&NBSP;FGETC (Open); if (ch == ' 1 ' | | ch == ' 0 ') { arr[i * n + j] = ch - ' 0 '; j++; //to prevent the spacing between rows and columns in the Maze } } } fclose (open);} Void print (int * arr, int n) //print Maze { for (int i = 0; i < max; i++) { for (int j = 0; j < max; j++) { cout << arr[i * n + j] << " "; } cout << endl; }}bool checkisaccess (Int * arr , int n, const pos& next) //Check the nodes on the next path of the maze node { ASSERT (arr); if (next._row >= 0 && next._row < MAX && next._col >= 0 && next._col <= max && arr[next._row * n + next._col] == 0) { return true; } return false;} entry is the location of the entrance, the paths used to preserve the maze//To determine if there is a pathway in the Maze bool searchmazepaths (int * arr, int N, pos entry, stack<pos> & paths) { assert (arr); paths.push (entry); while (! Paths.empty ()) { pos cur = paths.top (); arr[cur._row * n + cur._col] = 2; //changes the post-stack position if (cur._row == (n - 1)) { return true; } pos next = cur; //on next._row--; if (Checkisaccess (arr, n, next)) { paths.push (Next); continue; } //under next = cur; next._row++; if ( Checkisaccess (arr, n, next)) { paths.push (Next); continue; } //left next = cur; next._col--; if ( Checkisaccess (arr, n, next)) { paths.push (Next); continue; } //Right next = cur; next._col++; if (Checkisaccess (arr, n, next)) { paths.push (Next); continue; } paths.pop (); //if the remaining direction of the node can not be passed, then backtracking. }}
Test.cpp file #define _crt_secure_no_warnings 1//use stacks to design maze problems #include <iostream>using namespace std; #include <stdlib.h> #include "Memory.h" Int main () { int arr[MAX][MAX] = { 0 }; stack<pos> Paths; pos tmp; tmp._row = 2; Tmp._col = 0; getmaze ((int*) Arr, MAX); print ((int*) Arr, max); cout << endl < < endl; int ret = searchmazepaths ((int *) arr, max, tmp, paths) printf ("Can have a pathway:%d\n\n\n", ret); print ((int*) Arr, max); system (" Pause "); return 0;}
This article from "Unintentional persistent" blog, declined reprint!
Maze problem (Stack)