Project 2 in week 1-perform backtracking on the maze, and 2 in week 3-

Source: Internet
Author: User

Project 2 in week 1-perform backtracking on the maze, and 2 in week 3-

Problem:
In the Maze problem, when looking for a path, the usual method is: Starting from the entrance, testing along a certain direction, if you can pass, continue to forward; if you cannot get through, return along the original path, and continue testing in another direction until all possible possibilities are tested. To ensure that the original path can be returned (backtracking) at any position, you need to create a post-import, first-out stack to save the path from the entry to the current position.
In addition, the obtained path must be a simple path. That is, the same channel cannot be repeated in the obtained path.
To represent the maze, set an array. Each element indicates the status of a square. If it is 0, it indicates that the corresponding Square is a channel. If it is 1, it indicates that the corresponding Square is a wall. The array is as follows:

Int mg [10] [10] = {// defines a maze. 0 indicates the channel, and 1 indicates the wall,, }, }};
For each square in the maze, there are four squares adjacent to the top, bottom, left, and right. The position of the current square in column j of row I is (I, j), which indicates that the square above is 0, numbers are incremented clockwise. Assume that you can find the next square in the direction from 0 to 3.
In order to facilitate tracing, we need to stack all walking blocks and test their next walking orientation. We will save this walking orientation to the stack. The stack definition is as follows:

Struct St // define a stack and save the path {int I; // int j of the row number of the current square; // int di of the column number of the current square; // di is the next azimuth number} St [MaxSize]; // defines the stack

The solution path process is as follows: first import the portal to the stack (initial orientation is set to-1), and cycle when the stack is not empty: Take the top block of the stack (do not return the stack). If it is exit, the box in the output stack is the path. Otherwise, find the next adjacent square that can be taken. If such a square does not exist, return to the stack. If yes, save the orientation to the top element of the stack, and enter the adjacent block into the stack (the initial orientation is set to-1 ).
To ensure that the adjacent blocks are not blocks in the path, for example, (I, j) has been pushed into the stack, when the next block of the test (I + 1, j) can be taken, test (I, j) Again, which will lead to an endless loop. Therefore, after a block is pushed into the stack, change the corresponding mg array element value to-1 (which becomes unavailable ), when the stack is rolled back (there are no adjacent blocks available), it is restored to 0.

Source code:

<Span style = "font-size: 14px;" >#include <iostream> # include <iomanip> # include <cstdlib> using namespace std; # define MaxSize 100int maze [10] [10] = // defines a maze. 0 indicates the channel, and 1 indicates the wall }, {,}, {,}, {, 0,, ,}, {,}, {,}; struct Try // define a stack and save the path {int I; // int j of the row number of the current square; // int d of the column number of the current square; // di is the next azimuth number} path [MaxSize]; // defines the stack int top =-1; // initializes the stack pointer void findPath (int xb, int yb, int xe, int ye) // path from (xb, yb) to (xe, ye) {int I, j, d, find, k; top ++; // initial block into Stack path [top]. I = xb; path [top]. j = yb; path [top]. d =-1; maze [xb] [yb] =-1; while (top>-1) // loop when the stack is not empty {I = path [top]. i; j = path [top]. j; d = path [top]. d; if (I = xe & j = ye) // find the output Output path {cout <"maze path: \ n"; for (k = 0; k <= top; k ++) {cout <"\ t (" <path [k]. I <"," <path [k]. j <")"; if (k + 1) % 5 = 0) cout <endl; // change one line after each output of five squares} cout <endl; return;} find = 0; while (d <4 & find = 0) // find the next accessible vertex {d ++; switch (d) {case 0: // up I = path [top]. i-1; j = path [top]. j; break; case 1: // right I = path [top]. i; j = path [top]. j + 1; break; case 2: // down I = path [top]. I + 1; j = path [top]. j; Break; case 3: // left I = path [top]. i; j = path [top]. j-1; break;} if (maze [I] [j] = 0) find = 1; // find path} if (find = 1) // find the next square {path [top]. d = d; // modify the dvalue of the element on the top of the original stack. top ++; // you can go to the next block to go to the stack path [top]. I = I; path [top]. j = j; path [top]. d =-1; maze [I] [j] =-1; // avoid repeating this square. // cout <"\ t (" <path [top]. I <"," <path [top]. j <")"; // display the tested results} else // if no route is available, roll back to the stack {maze [path [top]. i] [path [top]. j] = 0; // change the path to another path. Path can go to top --;} cout <"no path available! \ N ";}int main () {findPath (,); // return 0 from ();} </span>
Running result:


@ Mayuko

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.