A * algorithm restriction algorithm (shortest path ???) code file 2

zhaozj2021-02-16  90

// find_path.cpp: importation of the find_path class.////

#include "stdafx.h" #include "find_path.h"

//// construction / destruction //

Find_path :: find_path () {_PF_GO_TEST = NULL;

Node_close = null; node_open = null; Node_Next = NULL;

PATH_P = NULL; Node_MAP = NULL;}

Find_path :: ~ FIND_PATH () {node_close = null; node_open = null; node_next = null;

IF (NULL! = PATH_P) {delete [] Path_p; Path_P = NULL;} f (null! = node_map) {delete [] node_map; node_map = null;}}

Void Find_Path :: RESET () {PATH_T = 0; MEMSET (Path_P, 0, Path_Max * SizeOf (Path_z));

MEMSET (Node_MAP, 0, PATH_MAX * SIZEOF (Path_Node)); for (int J = 0; j x = i; p-> y = j;}}}

Void find_path :: init (int width, int tent, pf_go_test pf) {set_pf (pf);

W = width; h = height; path_max = w * h; Path_P = new path_z [PATH_MAX]; Node_MAP = new path_node [PATH_MAX]; // reset ();}

// - Return to the shortest path step // - Return -1 means that the destination is not allowed to // - and store the shortest path to PATH_PINT FIND_PATH:: Go (int X0, int y0, int x1, INT Y1, PF_GO_TEST PF) {reset (); set_pf (pf); // reset (); node_open = (t *) (node_map) w * y0 x0; t * root; // root = node_open;

INT xx, yy; // - Decide the respective priority pattern of the X-axis or Y-axis, DY; / / - determines the priority page of the X-axis and the Y-axis BOOL FIND = FALSE; While (Null! = node_open) {root = getFromopenQueue (); if (null == root) Break; t & t = * root; if (x1 == TX && y1 == TY) {Find = true; Break; // reach the destination Success return} // - Decide Priority to seek ways IF (x1> tx) {xx = 1; DX = x1 - tx;} else {xx = 0; dx = Tx - x1;} // - if (Y1 > TY) {yy = 1; DY = Y1 - TY;} else {yy = 0; DY = TY - Y1;} // - Next priority // y direction priority IF (0 == dx || (0! = DY && DX> DY)) {// - x direction priority IF (1 == xx) {Trytile (TX - 1, Ty, Root); trytile (TX 1, Ty, Root); } // - x direction - priority else {trytile (TX 1, Ty, Root); Trytile (TX - 1, Ty, Root);} // - Y direction preferred IF (1 == yy) { Trytile (TX, TY - 1, ROOT); TRYTILE (TX, TY 1, ROOT);} // - y direction - Priority else {trytile (TX, TY 1, root); trytile (TX, TY - 1, root);}}} // - x direction priority else {// - y direction preferred IF (1 == yy) {Trytile (TX, TY - 1, ROOT); TRY TILE (TX, TY 1, ROOT);} // - y direction - Priority else {trytile (TX, TY 1, ROOT); TRYTILE (TX, TY - 1, ROOT);} // - x Direction Priority IF (1 == xx) {Trytile (TX - 1, Ty, Root); TRYTILE (TX 1, Ty, Root);} // - x Direction - Priority else {Trytile (TX 1, Ty, root; trytile (TX - 1, Ty, Root);}}}; if (false == find) {// - Unable to reach the destination Path_t = -1; return path_t;}

转载请注明原文地址:https://www.9cbs.com/read-14437.html

New Post(0)