I. Overview of the problem
When we were kids, we all played the maze game. Take a look at this legend:
When confronted with this problem, our first reaction is to find the entry point of the maze, and then search for the top and left four directions,
Detect whether the current position is a path, whether it can pass, until the exit location, is the correct trajectory of the maze. If you go to death
In the same location, you must return to the re-select path to go.
Let's simulate the maze problem, our maze is this:
haha ~ Although a little low. But to help us solve the actual problem, we will see, this more clearly explain the problem oh.
So where does this maze come from? I put it in a file to read the file and save its coordinates in a two-dimensional array.
Second, the solution
(1) Find the entry point (entry), which is given by our own coordinates.
(2) using heuristics to find the maze, there are up and down around four directions, the path will be marked, the value is assigned to other
Value. (I'll assign it to 2 here)
(3) If you walk into a dead end, use the thought of backtracking and pop it.
(4) Determine when to export the maze, when its horizontal axis is (line number-1), the description has been found export. (Of course, here you
You can also do not have to specify the exit at the bottom, depending on the legend of the Maze)
(5) If the maze is not exported, the stack is empty.
Third, the implementation of code
Maze.h
#include <iostream> using namespace std;
#include <assert.h> #include <stack> struct Pos {int _row;
int _col;
};
void Getmaze (int *maze,size_t N) {file* fout = fopen ("Maze.txt", "R");
ASSERT (Fout);
for (size_t i = 0, i < n; ++i) {for (size_t j = 0; J < N;)
{int value = FGETC (Fout);
if (value = = ' 1 ' | | value = = ' 0 ') {maze[i*n+j] = value-' 0 ';
++j; } else if (value = = EOF) {cout<< "Maze error!"
<<endl;
Return }}}} bool Ischeckpath (int *maze,size_t n,pos path) {if (path._row >= 0 && Path._row < ten) && Amp
(path._col >= 0 && Path._col <) && (Maze[path._row*n+path._col] = = 0)) {return true;
} return false;
}//Stack solver maze path void Getmazepath (int *maze,size_t n,pos entry,stack<pos>& s) {Pos cur = entry;
Pos next = cur;
Maze[cur._row*n+cur._col] = 2;
S.push (entry);
while (!s.empty ()) {if (S.top (). _row = = n-1) {return; }//DetectionUpper and lower left/next = S.top ();
Next._row-=1;
if (Ischeckpath (Maze,n,next)) {S.push (next);
Maze[next._row*n+next._col] = 2;
Continue
}//Right next = S.top ();
Next._col+=1;
if (Ischeckpath (Maze,n,next)) {S.push (next);
Maze[next._row*n+next._col] = 2;
Continue
}//Next Next = S.top ();
Next._row+=1;
if (Ischeckpath (Maze,n,next)) {S.push (next);
Maze[next._row*n+next._col] = 2;
Continue
}//Left next = S.top ();
Next._col-=1;
if (Ischeckpath (Maze,n,next)) {S.push (next);
Maze[next._row*n+next._col] = 2;
Continue
}//No channel Pos tmp = S.top ();
Maze[tmp._row*n+tmp._col] = 3;
S.pop ();
}}//recursive solver maze path void Getmazepath (int *maze,size_t n,pos entry,stack<pos>& s) {Pos next = entry;
Maze[next._row*n+next._col] = 2;
S.push (entry);
if (Next._row = = n-1) {return;
}//detection up and down//on next = entry;
Next._row-=1;
if (Ischeckpath (Maze,n,next)) {Getmazepath (maze,n,next,s);
}//Right Next = entry;
Next._col+=1;
if (Ischeckpath (Maze,n,next)) {Getmazepath (maze,n,next,s);
}//Lower next = entry;
Next._row+=1;
if (Ischeckpath (Maze,n,next)) {Getmazepath (maze,n,next,s);
}//Left next = entry;
Next._col-=1;
if (Ischeckpath (Maze,n,next)) {Getmazepath (maze,n,next,s);
}} void Printmaze (int *maze,size_t n) {cout<< "Maze display:>" <<endl;
for (size_t i = 0, i < n; ++i) {for (size_t j = 0; J < N; ++j) {cout<<maze[i*n+j]<< "";
} cout<<endl; }
}
Maze.cpp
#define _crt_secure_no_warnings 1
#include "Maze.h"
//stack and recursive implementation of the maze problem
const size_t N = ten;
void Funtest ()
{
int maze[n][n];
Pos entry = {2,0};
stack<pos> SS;
Getmaze ((int*) maze,n);
Getmazepath ((int*) maze,n,entry,ss);
cout<< "Whether the maze has exits. "<<!ss.empty () <<endl;
Printmaze ((int*) maze,n);
}
int main ()
{
funtest ();
return 0;
}
It is still possible to implement common maze problems with stacks and recursion, but there is one such maze:
When there are more than one path, it will involve the length of the problem, which path is the best way to go. The next blog to introduce you oh.
Would like to read this blog of the programming enthusiasts to put forward the opinion ha.