Traditional A * algorithm has an valuation function Int Judge (int X, int y)
// Valuation function, valuation, X, Y to the distance of the destination, the estimated value must be smaller than the actual value
This program algorithm assumes that the valuation function is estimated to be 0, and thus abandon the function Int Judge (int x, int y)
It is just a simple control of the way forward. Priority is preferred to the destination.
Simple test speed is faster, but it is not possible to ensure that the path to find the path must be shortest, and there is no complex road conditions to ensure that it can be successful (there should be no problem ^ _ ^)
// find_path.h: interface for the find_path class.////
#if! defined (AFX_FIND_PATH_H__6D68E153_6763_4EA4_81B7_8256024B5E23_8256024B5E23_8256024B5E23_8256024B5E23_6763_6D68E153_6763_4EA4_81B7_8256024B5E23__included_
#if _MSC_VER> 1000 # prgma overce # endif //_MSC_VER> 1000
#include
#include
// - Dogging algorithm (shortest path ???) // - From the A * algorithm evolved // 就 就 AB algorithm is good TypedEf class Find_Path Find_pathab; class find_path {// judgment a point Whether the function pointer can be reached define typedef bool (* pf_go_test) (int, int, "// - function pointer PF_GO_TEST _PF_GO_TEST; VOID SET_PF (PF_GO_TEST PF) {IF (PF) _PF_GO_TEST = PF;};
// - Determine if some point can arrive / / - require users to provide this function inline Bool Go_Test (int X, int y) {if (x <0 || y <0 || x> = W || y> = H) Return False;
// - IF (_pf_go_test) Return_PF_GO_TEST (X, Y);
// - Reach Return True by default;}
Public: void init, pf_go_test pf = null; void reset (); // - Return the shortest path step // - and store the shortest path to Path_P Int Go (int X0, int y0, int X1, int y1, pf_go_test pf = null);
PUBLIC: STRUCT PATH_Z {INT X, Y;}; int path_t; // - Found the shortest path step path_z * path_p; // - Found the shortest path
Private: enum {open = 0x01, close = 0x02, open_close = open | close,} __ const;
Struct path_node; typedef struct path_node t; struct path_node {short x, y; // - Node coordinate short flag; // - open / close flag
T * NEXT;
T * node_map; // - Each node in the map
T * node_close; // - Save the processed node (node_map) T * node_open; // - Save the node to be processed (Node_MAP) T * node_next; // - Node_open chain last one
INT W; / / - Map of width int h; // - High Int path_max; // - w * h
Public: // - Add Node to Open Table // - Advanced First Out (Non Sort) Inline Void AddToopenqueue (T * Node) {node-> flag = open; // Record Open flag IF (null == node_open) {Node_open = node; // node_next = node;} else {node_next-> Next = node; // node_next = node;} node_next = node;} // - Add Node from the OPEN Extract and add the node to the close table // - (Responsible to handle the peripheral node when tap) Inline T * getFromopenQueue () {if (null == node_open // || node_open-> flag & close // 不 可能 不 c 中) Return Null;
T * it = node_open;
// - Remove from the Open table // it-> flag = 0; node_open = node_open-> next;
// - Add Close Table IT-> Next = Node_Close; It-> Flag = Close; Node_Close = IT;
Return node_close;}
// - Try the next step to move to x, y Whether INLINE VOID TRYTILE (INT X, INT Y, T * PARENT) {t * it = node_map w * y x;
IF (// && // - Fast 50 == (IT-> Flag & Open_Close) // - Not in Open / Close Table && True == Go_Test (x, y) // This point can Arrival) {it-> parent = parent; addToopenqueue (IT); // Add node to open queue}}
PUBLIC: FIND_PATH (); // virtual ~ find_path ();
#ndif //! defined (AFX_FIND_PATH_H__6D68E153_6763_4EA4_81B7_8256024B5E23__INCLUDED_)