Maze Maze problem

Source: Internet
Author: User

Maze terrain We can look through the coordinates of a known entry to find the path by reading the file.

File

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/7F/13/wKiom1cSCnTB8pO4AAAZiAM5LuE146.png "title=" QQ picture 20160416174705.png "alt=" Wkiom1cscntb8po4aaaziam5lue146.png "/>

The location of each coordinate is recorded in POS:

struct Pos//position coordinates {int _row; int _col;};

Defining column Ranges

#define M 10//Line # define N 10//Column

Initialize the maze array :
Converts the characters obtained by reading a file into integer data, stored in an array of row n columns of M.

void Initmaze (int* maze) {struct wavheadhwav;   file* fout = fopen ("MazeMap.txt", "R"); The. txt file to be placed in the current directory if (fout = = NULL)//Here be sure to check if the read file is successful {cout << "can ' t find MazeMap.txt!" <&L T Endl<<endl;return;} for (int i = 0, i < M; i++) {for (int j = 0; J < N;) {char ch = fgetc (fout); if (ch== ' 1 ' | | ch== ' 0 ') {maze[i*n + j] = ch-' 0 '; j + +;}}} Fclose (fout);}

Backtrack lookup pathway :

Use the stack to store the path, through the upper and lower left and right four directions to traverse, if the position satisfies the conditions, it will be put into the stack, and the location of the data is set to 2, if the four directions do not meet the conditions of the stack operation, backtracking to find the location to meet the condition (if the stack is empty, indicating that the Until the exit is found.

Here to establish a minimum stack, if you find a way out, the stack is assigned to the minimum stack, and the exit is set to 1, continue to backtrack to find the path, if the path is found again, the length of the stack is compared with the minimum stack, if the stack length is smaller than the minimum stack, It will be assigned to the minimum stack once again (ensure that the minimum stack is the shortest path), continue to backtrack until the entire stack is empty, back to the entrance, the program ends.

Bool searchmazepath (int *maze, pos entry, stack<pos>& paths)     //  use stack backtracking to find the path and achieve the best solution {assert (Maze) of the maze, Stack<pos>min;paths.push (entry);while  (! Paths.empty ()) {pos cur = paths.top ();maze[cur._row*n+cur._col] = 2;if  (cur._row= =m-1) {if  (Paths.size () < min.size ()  | |  min.size ()  == 0) {min = paths;} Paths.pop (); Maze[min.top (). _row*n + min.top (). _col] = 1;} pos next = cur;next._col--;  //left if  (checkisaccess (Maze, next)) {Paths.push ( next); maze[next._row*n + next._col] = 2;continue;} next = cur;next._col++; //Right if  (checkisaccess (Maze, next)) {Paths.push (next); Maze[next. _row*n + next._col] = 2;continue;} next = cur;next._row--; //on if  (checkisaccess (Maze, next)) {Paths.push (next); Maze[next. _row*n + next._col] =&nbSp;2;continue;} next = cur;next._row++; //if  (checkisaccess (Maze, next)) {Paths.push (next); Maze[next. _row*n + next._col] = 2;continue;} Paths.pop ();} if  (Paths.empty () &&!min.empty ()) {maze[min.top (). _row*n + min.top () ._col] = 2; Return true;} Return false;}

Check that the location is legal: (coordinates within the row and column range)

BOOL Checkisaccess (int* Maze, const pos& next)//check that the location is valid {assert (maze); if (next._row >= 0 && Next._ro W <= N) && (next._col >= 0 && next._col < M) && Maze[next._row*n + next._col] = = 0) {retur n true;} return false;}

Print Maze:

void Printmaze (int *maze)//print Maze {assert (Maze), for (int i = 0, i < M; i++) {for (int j = 0; J < N; J + +) {cout <& Lt Maze[i*n + j] << "";} cout << Endl;}  cout << Endl; }

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M01/7F/13/wKiom1cSE4uRNawYAAAnq-ygG7I222.png "title=" capture. PNG "alt=" Wkiom1cse4urnawyaaanq-ygg7i222.png "/>

This article from "Yan Anyang" blog, declined reproduced!

Maze Maze problem

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.