Solving the maze problem of data structure (ii) the shortest path of the maze

Source: Internet
Author: User

In the previous article we discussed the problem of the common solution to the maze problem, this article we continue to go deep, to find the shortest path of the maze.

To find the shortest path of the maze, a very simple way is to set a min stack, used to put the shortest path, each found an exit, the path stack and min stack comparison, if the path stack is smaller, then assign to Min.

In the previous article, we marked the path we had traveled, and, with each coordinate, set that coordinate to 3 until we found the exit.

So if you use this type of tagging, it's obvious that there will be problems.

So we need to change the way of marking!

Eventually.... I decide that the value of the exit is 2, and each step makes the current position marker the last position marker plus 1.

In this case, we detect whether the current position can pass the function (Checkisaccess (INT*,SZ,POS)) need to do some minor adjustments ....

Detection pathway function
/** function: Detect whether the current path can pass (shortest path) * Parameter description: *    Maze: Maze array *      SZ: Maze size *     cur: Current position coordinates *    Next: Next position coordinates * Return value: Can be returned by true, Cannot be returned by False*/bool checkisaccess (int *maze,size_t sz,pos Cur,pos next) {//If the next path crosses the IF ((next.x<0| | NEXT.X>SZ) | | (next.y<0| | Next.y>sz)) {return false;} The next coordinate is 0if (0 = = (Maze[next.x*sz+next.y])) {return true;} The next coordinate is the path that was previously traversed if ((maze[next.x*sz+next.y]>maze[cur.x*sz+cur.y]+1)) {return true;} return false;}
Finding the shortest Path function
/* Function: Find Maze Shortest Path * parameter description: * Maze: Maze array * SZ: Maze size * Entry: Maze entry * Path: traversed path * Min: Shortest Path * return value: Can be returned by true, cannot be returned by false*// /Find the shortest path void Getmazeminpath (int *maze,size_t sz,pos &entry,stack<Pos>& path,stack<pos> &min) { Path.push (entry); Pos cur = entry; Pos next = cur;//Find exit if (sz-1 = = cur.y) {//First assignment to min or path path shorter than min if (min.empty () | | | Path.size () <min.size ()) {Min = path;} Path.pop (); return;} Right Next.x + = 1;if (checkisaccess (Maze,sz,cur,next)) {
MAZE[NEXT.X*SZ+NEXT.Y] = maze[cur.x*sz+cur.y]+1; Getmazeminpath (maze,sz,next,path,min);} Left next = cur;next.x-= 1;if (checkisaccess (Maze,sz,cur,next)) {Maze[next.x*sz+next.y] = maze[cur.x*sz+cur.y]+1; Getmazeminpath (maze,sz,next,path,min);} On next = Cur;next.y + = 1;if (checkisaccess (Maze,sz,cur,next)) {Maze[next.x*sz+next.y] = maze[cur.x*sz+cur.y]+1; Getmazeminpath (maze,sz,next,path,min);} Lower next = cur;next.y-= 1;if (checkisaccess (Maze,sz,cur,next)) {Maze[next.x*sz+next.y] = maze[cur.x*sz+cur.y]+1; Getmazeminpath (maze,sz,next,path,min);} Path.pop ();}
Full code
#ifndef __maze_h__#define __maze_h__#include<iostream> #include <iomanip> #include <stack> #include <assert.h>namespace maze{using namespace std;//maze size static const int N = 10;//Maze Map file name static const char *const Filenam E = "MazeMap.txt";//coordinate struct Pos{int x;//horizontal axis (essentially array arr[i][j] j) int y;//ordinate (essentially array arr[i][j] i)};/* function Description: Get the Maze map parameter description from the file: Maze: Maze map array sz: Maze size return value: No */void getmaze (int *maze,size_t sz) {FILE *FP = fopen (FILENAME, "R");//Open Failed if (NULL==FP) {// Output error message perror (FILENAME);//End Program exit (1);} Read the maze map in the file into the Maze array for (size_t i=0; i<sz; ++i) {for (size_t j=0; j<sz;) {//Get characters from the file stream char tmp = GETC (FP);//The character is 0 or 1 o'clock, Import array if (tmp== ' 0 ' | | tmp== ' 1 ') {maze[i*sz+j]=tmp-' 0 '; ++j;} else if (eof==tmp) {//file has been read, loop has not stopped//description There is a problem with the maze map in the file assert (false); return;}} Close file fclose (FP);} /* Function Description: Print Maze parameter description: Maze: Maze map array sz: Maze size return value: No */void printmaze (int *maze,size_t sz) {COUT&LT;&LT;SETW (3); for (size_t i=0; i& Lt;sz; ++i) {for (size_t j=0; j<sz; ++j) {COUT&LT;&LT;MAZE[I*SZ+J]&LT;&LT;SETW (3);} Cout<<endl;}} /* Function Description: Detect current bitParameter description: Maze: Maze map array sz: Maze size cur: Current location return value: Can be returned by true, cannot be returned by False.*/bool checkisaccess (int *maze,size_t Sz,pos cur) {if (cur.x>=0 && cur.x<sz &&//Line coordinates are out of bounds cur.y>=0 && Cur.y<sz &&// Whether the column coordinates are out of bounds maze[cur.x*sz+cur.y]==0) {//whether the row can pass return true;} return false;} /* Function Description: Maze solver via stack parameter description: Maze: Maze map array sz: Maze Size entry: Maze entry point path: Stack return value for finding the maze exit: Find Exit return True, no return false.*/bool Findmazepath (int *maze,size_t sz,pos &entry,stack<Pos>& path) {//will enter the stack Path.push (entry);//If the stack is not empty while (!path.empty ()) {//Gets the top element of the stack, that is, the path of the last walk pos cur = path.top ();//mark it as traversed maze[cur.x* SZ+CUR.Y] = 3;//Find exit if (sz-1==cur.x) {return true;} Pos next = cur;//Next, move right next.x + = 1;if (checkisaccess (Maze,sz,next)) {//can move right, will current step into stack Path.push (next); continue;} Next = cur;//Next, move left next.x-= 1;if (checkisaccess (Maze,sz,next)) {//can move to the left, into the stack Path.push (next); continue;} Next, move up next = Cur;next.y + = 1;if (checkisaccess (Maze,sz,next)) {//can move up Path.push (next); continue;} Next = cur;//moves down Next.y-= 1;if (Checkisaccess (Maze,sz,nexT) {//can move down Path.push (next); continue;} Up, down, left, right can not go Path.pop ();} return false;} /** function Description: Search for Maze exit by recursion * Parameter description * Maze: Maze map * SZ: Maze size *entry: Maze entry * Path: Used to determine if there is an exit stack * return value: None (if there is an exit, the stack is empty, if there is no exit, there is a start coordinate in the stack) */ void findmazepathr (int *maze,size_t sz,pos &entry,stack<Pos> & Path) {//pushes the entry stack Path.push (entry); Pos cur = entry;//will have traversed the road marked as 3maze[cur.x*sz+cur.y] = 3;//Find the exit, return directly if (sz-1==entry.x) {//start coordinates pop up path.pop (); return;} Pos next = cur;//right Next.x + = 1;if (checkisaccess (Maze,sz,next)) {//start with the current position, recursively proceed to the next findmazepathr (Maze,sz,next,path);} Next = cur;//Left Next.x-= 1;if (checkisaccess (Maze,sz,next)) {findmazepathr (Maze,sz,next,path);} Upper Next = cur;next.y + = 1;if (checkisaccess (Maze,sz,next)) {findmazepathr (Maze,sz,next,path);} Under next = cur;next.y-= 1;if (checkisaccess (Maze,sz,next)) {findmazepathr (Maze,sz,next,path);} Path.pop ();} /** function: Detect whether the current path can pass (shortest path) * Parameter description: * Maze: Maze array * SZ: Maze size * cur: Current position coordinates * Next: Next position coordinates * Return value: Can be returned by true, cannot be returned by false*/ BOOL Checkisaccess (int *maze,size_t sz,pos Cur,pos next) {//If Next pathOut of Bounds if (next.x<0| | NEXT.X&GT;SZ) | | (next.y<0| | Next.y>sz)) {return false;} The next coordinate is 0if (0 = = (Maze[next.x*sz+next.y])) {return true;} The next coordinate is the path that was previously traversed if ((maze[next.x*sz+next.y]>maze[cur.x*sz+cur.y]+1)) {return true;} return false;} /** function: Find Maze Shortest Path * parameter description: * Maze: Maze array * SZ: Maze size * Entry: Maze entry * Path: traversed path * Min: Shortest Path * return value: Can be returned by true, not by returning false*/ Find Shortest path void getmazeminpath (int *maze,size_t sz,pos &entry,stack<Pos>& path,stack<pos> &min) { Path.push (entry); Pos cur = entry; Pos next = cur;//Find exit if (sz-1 = = cur.y) {//First assignment to min or path path shorter than min if (min.empty () | | | Path.size () <min.size ()) {Min = path;} Path.pop (); return;} Right Next.x + = 1;if (checkisaccess (Maze,sz,cur,next)) {Maze[next.x*sz+next.y] = maze[cur.x*sz+cur.y]+1; Getmazeminpath (maze,sz,next,path,min);} Left next = cur;next.x-= 1;if (checkisaccess (Maze,sz,cur,next)) {Maze[next.x*sz+next.y] = maze[cur.x*sz+cur.y]+1; Getmazeminpath (maze,sz,next,path,min);} On next = Cur;next.y + = 1;if (checkisaccess (Maze,sz,cur,next)) {Maze[next.x*sz+NEXT.Y] = maze[cur.x*sz+cur.y]+1; Getmazeminpath (maze,sz,next,path,min);} Lower next = cur;next.y-= 1;if (checkisaccess (Maze,sz,cur,next)) {Maze[next.x*sz+next.y] = maze[cur.x*sz+cur.y]+1; Getmazeminpath (maze,sz,next,path,min);} Path.pop ();}} #endif
Test code
#include "Maze.h" using namespace Maze;void mazetest () {int arr[n][n];//maze map Pos entry = {2,0};//start coordinates stack<pos> path ;//stack stack<pos> min;//Shortest path Getmaze ((int *) arr,n);//import a maze of files into arr array arr[2][0] = 2;//mark the entry as 2PrintMaze ((int *) arr,n )///Print Maze Getmazeminpath ((int *) arr,n,entry,path,min);//Find the Maze exit cout<<endl<<endl;//line handling (make the interface more tidy) Printmaze (int *) arr,n);//print through the Maze}int main () {mazetest (); return 0;}

Summary: This thing, or pretty need to study deeply, especially for those who are not good enough to understand the recursive procedures, the proposal, step-by-step follow-up procedures, complete the process. Of course, the shortest path to the maze, this is obviously not the only way, more ways to discuss it later!

Attached Project Document: Http://pan.baidu.com/s/1eSJr1YE (VS2010)

Solving the maze problem of data structure (ii) the shortest path of the maze

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.