A * algorithm Wayfinding algorithm (shortest path??) Code Document 1

  Traditional A * algorithm is a valuation function int judge (int x, int y) 

  / / Function valuation, the valuation x, y distance to the destination, with an estimated value than the actual value must ensure that small 

  This procedure algorithm assumptions valuation function for the total assessed value of 0, thus discarding function int judge (int x, int y) 

  It's a simple control Wayfinding direction to the priority destinations around. 

  Simple test faster, but can not guarantee that the Wayfinding certain it is the shortest path, through a complicated road test does not guarantee success Wayfinding (should be no problem ^ _ ^) 

  / / Find_path.h: interface for the find_path class. 
  / / 
  ////////////////////////////////////////////////// //////////////////// 

  # If! Defined (AFX_FIND_PATH_H__6D68E153_6763_4EA4_81B7_8256024B5E23__INCLUDED_) 
  # Define AFX_FIND_PATH_H__6D68E153_6763_4EA4_81B7_8256024B5E23__INCLUDED_ 

  # If _MSC_VER> 1000 
  # Pragma once 
  # Endif / / _MSC_VER> 1000 

  # Include 
  # Include 

  / / - Wayfinding algorithm (shortest path??) 
  / / - From the A * algorithm evolution of 
  / / - AB algorithm called good 
  Typedef class find_path find_pathAB; 
  Class find_path 
  ( 
  / / - Judge can reach a point of the function pointer definition 
  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;); 

  / / - Judge can reach a point 
  / / - This function requires users to provide 
  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); 

  / / - Defaults can be reached 
  Return true; 
  ) 

  Public: 
  Void init (int width, int height, pf_go_test pf = NULL); 
  Void reset (); 
  / / - Back to find the shortest path step 
  / / - And find the shortest path stored in the 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; / / - finding the shortest path step 
  Path_z * path_p; / / - finding the shortest path 

  Private: 
  Enum 
  ( 
  OPEN = 0×01, 
  CLOSE = 0×02, 
  OPEN_CLOSE = OPEN | CLOSE, 
  __CONST); 

  Struct path_node; 
  Typedef struct path_node T; 
  Struct path_node 
  ( 
  Short x, y; / / - node coordinates 
  Short flag; / / –Open/Close signs 

  T * parent; 
  T * next; 
  ); 

  T * node_map; / / - each node in the corresponding map 

  T * node_close; / / - dealt with the preservation of the node (node_map) 
  T * node_open; / / - keeping the nodes to be processed (node_map) 
  T * node_next; / / - node_open chain last 

  Int w; / / - MAP width 
  Int h; / / - height map 
  Int path_max; / / - w * h 

  Public: 
  / / - Table node join OPEN 
  / / - FIFO (not sort) 
  Inline void AddToOpenQueue (T * node) 
  ( 
  Node-> flag = OPEN; / / Open Records signs 
  If (NULL == node_open) 
  ( 
  Node_open = node; 
  / / Node_next = node; 
  ) 
  Else 
  ( 
  Node_next-> next = node; 
  / / Node_next = node; 
  ) 
  Node_next = node; 
  ) 

  / / - OPEN from table to join nodes and node table CLOSE 
  //–( Take charge of dealing with neighboring nodes, nodes) 
  GetFromOpenQueue inline T * () 
  ( 
  If (NULL == node_open 
  / / | | Node_open-> flag & CLOSE / / - not in the Close 
  ) Return NULL; 

  T * = node_open it; 

  / / - Table removed from the OPEN 
  / / It-> flag = 0; 
  Node_open = node_open-> next; 

  / / - Join CLOSE Table 
  It-> = node_close next; 
  It-> flag = CLOSE; 
  Node_close = it; 

  Return node_close; 
  ) 

  / / - Try to move to the next step x, y not feasible 
  Inline void TryTile (int x, int y, T * parent) 
  ( 
  It node_map T * = * y + w + x; 

  If ( 
  / / & & 
  / / - Relatively fast speed 
  0 == (it-> flag & OPEN_CLOSE )//– not OPEN / CLOSE table 
  & & 
  Go_test true == (x, y) / / - can reach the point 
)
  ( 
  It-> parent = parent; 
  AddToOpenQueue (it); / / - Open nodes join queue 
  ) 
  ) 

  Public: 
  Find_path (); 
  / / Virtual 
  ~ Find_path (); 
  ); 

  # Endif / /! Defined (AFX_FIND_PATH_H__6D68E153_6763_4EA4_81B7_8256024B5E23__INCLUDED_) 

Bookmark it: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • Sphinn
  • del.icio.us
  • Google
  • DotNetKicks
  • DZone
  • Furl
  • Netvouz

Tags:

Releated Articles


0 Comments to “A * algorithm Wayfinding algorithm (shortest path??) Code Document 1”

No Comments. Send your comment.

Leave a Reply

You must be logged in to post a comment.