It is a classic programming problem to find all the paths from the entrance to the exit of the maze. The general design idea is to start from the entrance, down a certain direction to explore, explore divided into the upper and lower left and right four directions, which direction is to go down, if each direction can not go down on the original road " back ." Therefore, a last-in-first-out structure is required to hold the path from the ingress to the exit. So the use of the stack to achieve is very convenient, go in a certain direction, each of the available position into the stack tag, and then switch to the next position, if not all, the stack top out of the stack to cancel the tag, and then look for. Come down. A simple maze solver problem (a path is solved), as for solving multiple paths and finding the shortest path problem I am still in the further study.
Let's take a look at how to dynamically open up a two-dimensional array before giving the code.
int *a = new int[n*n];int** a=new Int*[n];//a[i][j] equivalent to A[i*n + j];
Okay, now look at the concrete implementation of the Maze:
#define _CRT_SECURE_NO_WARNING#pragma once#include<iostream> #include <stack> #include < assert>using namespace std; #define &NBSP;N&NBSP;10STRUCT&NBSP;POS{POS (size_t row, size_t col): _row (Row), _col (COL) {}int _row;int _col;}; Void getmaze (int *a,int n)//read into file {file* fout = fopen ("MazeMap.txt", "R"); ASSERT (Fout);for (int i = 0; i < n; i++) {for (int j = 0; j < n;) {char ch = fgetc (fout);if (' 0 ' == ch | | ' 1 ' == ch) {a[i*n + j] = ch - ' 0 '; j + +;} else{continue;}}} Fclose (fout);} Void printmaze (int* a,int n) {for (int i = 0; i < n; i++) {for (int j = 0; j < n; j++) {Cout << a[i*n + j]<< " ";} cout << Endl;}} Bool checkisaccess (Int* a, int n, pos next) {assert (a);if (next._row >= 0) && (next._row < n) && (next._col >= 0) && (next._col < n) && (a[next._row*n+next._col]==0)) {return true;} Else{return false;}} Bool mazepath (int* a, int n, const pos& entry, stack<pos> & path) {pos cur = entry;//entry location Path.push (cur);while (!path.empty ()) {//Whether it has been to the exit if (cur._row == n - 1) {a[cur._row*n + cur._col] = 2;return true;} a[cur._row*n + cur._col] = 2;//***************************************** on Pos next = cur;next._row--;if (Checkisaccess (A, n, next)) {Cur = next;path.push (cur) ; continue;} Next NEXT&NBSP;=&NBSP;CUR;NEXT._ROW++;IF&NBSP; (checkisaccess (A, n, next)) {Cur = next;path.push (cur); continue;} Left next = cur;//left next._col--;if (Checkisaccess (A, n, next) {cur = next;path.push (cur); continue;} Right next = cur;next._col++;if (checkisaccess (A, n, next) {cur = next;path.push (cur); continue;} Cur = path.top ();//To this indicates no access, so stack top out stack path.pop ();} Return false;} Void testmaze () {int a[n][n] = {}; Getmaze ((int *) a,n); Printmaze ((int *) a, n);stack<pos> path; Pos entry = { 2, 0 };bool ret = mazepath (int *) A, N, entry, path);cout << "Is there a pathway?" " << ret << endl; Printmaze ((int *) a, n);}
650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M02/7E/E0/wKioL1cLmuvyQTNpAAAuTmotOqo431.png "title=" capture. PNG "alt=" Wkiol1clmuvyqtnpaaautmotoqo431.png "/>
When the maze has multiple exits, the shortest path can be obtained using the global stack. We'll discuss this next time 650) this.width=650; "src=" Http://img.baidu.com/hi/jx2/j_0063.gif "alt=" J_0063.gif "/>
This article is from the "stand out or Get out" blog, so be sure to keep this source http://jiazhenzhen.blog.51cto.com/10781724/1762752
Stack solving maze problem