Tracing to find the shortest path of a maze

Source: Internet
Author: User

There is a two-dimensional array. 0 represents the path, and-1 represents the wall. Find the shortest path of any two points.

Let's first look at how to find a path: finding a two-point path is a typical Maze problem in the data structure. The solution is as follows:

Start from a point and look for the four directions (top, right, bottom, left). In each step, add the value of the point that has elapsed to 1 to prevent repeated walking, and press the vertices that have passed into the stack (indicating the path). If there is a wall or the vertices that have passed, the system cannot move forward. If there is no way forward, the system returns and the path is rolled back to the stack, in this way, call recursively until the end point is found.

If we change the search order to left, right, top, and bottom, we may get a shorter path, but this method cannot be the shortest path.

You can obtain the shortest path by searching and reading books online:

FollowLeft, right, top, bottomDirection. Each step is taken, the value of the node to be taken is assigned to this node value + 1, and the path you walk can also be repeated. The condition is that the value of the current node + 1 must be smaller than the value of the existing node (the wall cannot go), so that all nodes can be traversed and the shortest path can be recorded.


The C ++ code is implemented as follows:

# Include <iostream> # include <iomanip> # include <stack> using namespace STD; Class maze {public: // member variable initialize maze (size_t start_x, size_t start_y, size_t end_x, size_t end_y): m_start (node (start_x, start_y), m_end (node (end_x, end_y) {m_maze [start_x] [start_y] = 1; // The entrance to the maze is initialized to 1 to avoid repeated steps} // find the shortest path void searchpath () {searchpath (m_temppath, m_start, m_end ); // call the private function} // print the void printpath () {While (! M_path.empty () {cout <'(' <m_path.top (). m_x <',' <m_path.top (). m_y <')' <"-->" <m_maze [m_path.top (). m_x] [m_path.top (). m_y] <Endl; m_path.pop () ;}// print the array node value after traversal void printmaze () const {for (size_t I = 0; I <10; ++ I) {for (size_t J = 0; j <10; ++ J) {cout <SETW (2) <m_maze [I] [J] <',';} cout <'\ B' <Endl ;}} PRIVATE: // defines the node class node {public: int m_x; int m_y; node (INT x = 0, int y = 0): m_x (x), m_y (y) {} const node operator + (const node & that) const {node; node. m_x = m_x + that. m_x; node. m_y = m_y + that. m_y; return node ;}}; // search for void searchpath (stack <node> & Path, const node & START, const node & End) {If (start. m_x = end. m_x & start. m_y = end. m_y) // go to the end {If (path. size () <m_path.size () // The current path is shorter or is the first path found | m_path.empty () m_path = path; return;} For (size_t I = 0; I <4; ++ I) // traverse four directions {node nextnode = start + offset [I]; // Add the offset if (iscango (START, nextnode )) // If you can use {m_maze [nextnode. m_x] [nextnode. m_y] = m_maze [start. m_x] [start. m_y] + 1; Path. push (nextnode); // The searchpath (path, nextnode, end) of the node's inbound stack; // recursively searches for path. pop (); // The top of the pop-up stack }}// determine whether start to next can pass through bool iscango (const node & START, const node & next) const {int x = next. m_x, y = next. m_y; int prevalue = m_maze [start. m_x] [start. m_y]; // If (x <0 | x> 9 | Y <0 | Y> 9 // out of bounds |-1 = m_maze [x] [Y]) // wall return false; If (0 = m_maze [x] [Y]) // return true; else return prevalue + 1 <m_maze [x] [Y]; // past vertex} stack <node> m_path; // store the Shortest Path stack <node> m_temppath; // store the temporary path node m_start; // node m_end in the Maze entry; // maze exit static int m_maze [10] [10]; // maze static node offset [4]; // four direction offset}; int maze :: m_maze [] [10] = {0, 0, 0, 0,-1, 0, 0, 0, 0}, {0,-1,-1, 0, 0, 0, 0,-1, 0, 0}, {0, 0,-1, 0,-1, 0, 0,-1, 0, -1}, {0, 0,-1, 0,-1, 0, 0,-1, 0,-1}, {0, 0, 0, 0, -1,-1, 0,-1, 0, 0}, {0, 0,-1, 0, 0, 0, 0, 0, 0 }, {0,-1, 0, 0,-1, 0,-1,-1, 0, 0}, {0, 0, 0,-1, 0, 0, 0,-1, 0,-1}, {-1, 0, 0,-1, 0, 0,-1, 0,-1}, {0, 0, 0, 0, 0, 0, 0, 0, 0,-1 }}; maze: node maze: Offset [] = {node (-1, 0), node (1, 0), node (0,-1), node (0, 1)}; int main (void) {maze (2, 1, 1, 8); maze. searchpath (); maze. printpath (); maze. printmaze (); Return 0 ;}
The result is as follows: (the number after the coordinate is the number of nodes that have passed through)

Tracing to find the shortest path of a 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.