Use the astar algorithm to solve the labyrinth shortest path problem

Source: Internet
Author: User

I wrote this maze for 7 rows, 7 columns, and 7 columns. The write is urgent. I just want to calculate the maze of N rows and M columns.

The Code is as follows:

# Include <iostream> # include <set> # include <string> # include <cmath> # include <cstdlib> # define n 100 using namespace STD; struct node {double H, g, F; int X, Y; bool operator <(node other) const {return F <Other. f ;}}; int map [N] [N]; // The Maze. 1 indicates that the path can be taken. 0 indicates that the path cannot go through int row or Col; // et <node> open; // you can expand the node Multiset <node>: iterator it; node close [N] [N]; // The node that has passed the int step [8] [2] = {,-, 0,-,-1,-,-1, -1}; // method I NT Prev [N * n]; // store the previous node void Init () {memset (close, 0, sizeof (close);} double getg (node, node B) // obtain the PATH value {return SQRT (1.0 * (. x-b.x) * (. x-b.x) +. y-b.y) * (. y-b.y);} bool is_match (node A, Node B) {if (. X = B. X &. y = B. y) {return true;} return false;} node begin, end; int main () {While (CIN> row> col) {open. clear (); Init (); For (INT I = 0; I <row; I ++) // input the maze {for (Int J = 0; j <Col; j ++) {CIN> map [I] [J] ;}} CIN> begin. x> B Egin. y> end. x> end. y; // start point and end point begin. G = getg (begin, end); // initialize the starting point of the Value Function begin. F = begin. g; begin. H = 0; open. insert (BEGIN); // bool issuccess = false; while (! Open. empty () // but there are still nodes that can go through {node son = * (open. begin (); // extracts the path consumption + the node open with the smallest valuation function. erase (open. begin (); // Delete the above node from the Open table for (INT I = 0; I <8; I ++) // search for 8 directions {int u = Son. X + step [I] [0]; int v = Son. Y + step [I] [1]; if (u> = 0 & V> = 0 & U <row & V <Col & map [u] [v]) // If the node is in the matrix and the value is 1, run {// to update the node information, including the path consumption and valuation function node TMP; TMP. X = u; TMP. y = V; TMP. G = getg (TMP, end); TMP. H = Son. H + 1; TMP. F = TMP. H + TMP. g; bool isinopen = false; // indicates whether the IF (u) = End. X & V = end. y) // if it is the end point, exit the loop {Prev [u * 7 + V] = Son. x * 7 + son. y; // record the previous node issuccess = true; // indicates that the path goto breakpoint is successfully found;} For (IT = open. begin (); it! = Open. end (); It ++) {// If the TMP point is in the Open table, compare it with the F value in the Open table, if the former F is small, it inserts TMP into the Open table if (is_match (* It, TMP) {isinopen = true; // indicates if (* It) in the Open table ). f> TMP. f) {node new = TMP; open. erase (it); open. insert (TMP); Prev [TMP. x * 7 + TMP. y] = Son. x * 7 + son. y; // record the previous node break; }}} bool isinclose = false; // mark whether it is in the close table if (close [TMP. x] [TMP. y]. f) // if it is in close, it indicates that it has passed through and then compares the size of F. If the former F is small, insert the vertex into the Open table and delete the vertex from the close table. {Isinclose = true; // If (TMP. F <close [TMP. x] [TMP. y]. f) {open. insert (TMP); close [TMP. x] [TMP. y]. f = 0; close [TMP. x] [TMP. y]. G = 0; close [TMP. x] [TMP. y]. H = 0; Prev [TMP. x * 7 + TMP. y] = Son. x * 7 + son. y; // record the previous node} If (! Isinopen &&! Isinclose) // If the node is neither in the close table nor in the Open table, it indicates that the node has not passed and is inserted into the Open table. {Open. insert (TMP); Prev [TMP. x * 7 + TMP. y] = Son. x * 7 + son. y; // record the previous node }}// put the point retrieved from the Open table into the close table, which indicates that the node has passed through the close [son. x] [son. y]. X = Son. x; close [son. x] [son. y]. y = Son. y; close [son. x] [son. y]. G = Son. g; close [son. x] [son. y]. F = Son. f; close [son. x] [son. y]. H = Son. h;} breakpoint:; If (issuccess) // if the path is found, the output path is not output in reverse order, so the point is inverted. {Cout <"The path is:" <Endl; int x = end. x * 7 + end. y; int y = begin. x * 7 + begin. y; while (X! = Y) {cout <"(" <X/7 <"," <X % 7 <") --> "; X = Prev [X];} cout <"(" <begin. x <"," <begin. Y <")" <Endl; cout <"reach success! "<Endl;} else // cannot reach the end {cout <" reach failed! "<Endl ;}}}

 

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.