A星算法

来源:互联网 发布:雪菜碧池 知乎 编辑:程序博客网 时间:2024/05/19 05:05

A星算法是种启发式算法
所谓的”启发式”,就是对每一个搜索的位置进行评估,也就是把找的位置离目标的距离当成找点的一个依据,然后猜测这个点是否最佳(“启发式”就是猜测)。

为了找到最佳的那个点
可以规定:
G = 从起点,沿着产生的路径,移动到网格上指定方格的距离。
H = 从网格上那个方格移动到终点B的预估移动距离。
这里写图片描述
F = G + H
F最小的点可以认为是该选的点
有兴趣的可以看看这篇文章
http://www.policyalmanac.org/games/aStarTutorial.htm

下面就是我用C++实现的寻步

#include<iostream>#include<stack>#include <queue>#include <cstdlib>#include <cmath>#include<stack>#include<mem.h>#define STARTNODE   1  #define ENDNODE     2  #define BARRIER     3 using namespace std;struct point{    int x;    int y;    int g,h,f;    int style;    struct point *par;    int in_close;    int in_open; }p,*ppoint;point map_maze[5][15];ppoint open[75];ppoint close[75];int close_node_count; // close表中结点数量int open_node_count;point path_stack[5][15];void swap( int idx1, int idx2 )    {        ppoint tmp = open[idx1];      open[idx1] = open[idx2];      open[idx2] = tmp;  }    int main()  {       // 地图数组的定义      //       point *start_node;          // 起始点      point *end_node;            // 结束点      point *curr_node;           // 当前点      int       is_found;         // 是否找到路径      int maze[5][15] ={           // 仅仅为了好赋值给map_maze                          { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },                        { 0,0,0,0,0,0,0,3,0,0,0,0,0,0,0 },                         { 0,0,1,0,0,0,0,3,0,0,0,0,2,0,0 },                         { 0,0,0,0,0,0,0,3,0,0,0,0,0,0,0 },                         { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },                       };      int       i,j,x;      // 下面准备点      //       for( i = 0; i < 5; ++i )      {          for ( j = 0; j < 15; ++j )          {              map_maze[i][j].g = 0;              map_maze[i][j].h = 0;              map_maze[i][j].is_in_close = 0;              map_maze[i][j].is_in_open = 0;              map_maze[i][j].s_style = maze[i][j];              map_maze[i][j].x = i;              map_maze[i][j].y = j;              map_maze[i][j].par = NULL;              if ( map_maze[i][j].style == STARTNODE )  // 起点              {                  start_node = &(map_maze[i][j]);              }              else if( map_maze[i][j].style == ENDNODE )    // 终点              {                  end_node = &(map_maze[i][j]);              }              cout<<maze[i][j];            //printf("%d ", maze[i][j]);          }          cout<<endl;        //printf("\n");      }      // 下面使用A*算法得到路径      //        open_table[open_node_count++] = start_node;         // 起始点加入open表      start_node->is_in_opentable = 1;               // 加入open表      start_node->s_g = 0;      start_node->s_h = abs(end_node->x - start_node->x) + abs(end_node->y - start_node->y);      start_node->s_parent = NULL;      if ( start_node->x == end_node->x && start_node->y == end_node->y )      {         // printf("起点==终点!\n");          return 0;      }      is_found = 0;      while( 1 )      {          curr_node = open_table[0];      // open表的第一个点一定是f值最小的点(通过堆排序得到的)          open_table[0] = open_table[--open_node_count];  // 最后一个点放到第一个点,然后进行堆调整          adjust_heap( 0 );               // 调整堆          close_table[close_node_count++] = curr_node;    // 当前点加入close表          curr_node->s_is_in_closetable = 1;       // 已经在close表中了          if ( curr_node->s_x == end_node->s_x && curr_node->s_y == end_node->s_y )// 终点在close中,结束          {              is_found = 1;              break;          }          get_neighbors( curr_node, end_node );           // 对邻居的处理          if ( open_node_count == 0 )             // 没有路径到达          {              is_found = 0;              break;          }      }      if ( is_found )      {          curr_node = end_node;          while( curr_node )          {              path_stack[++top] = curr_node;              curr_node = curr_node->s_parent;          }          while( top >= 0 )        // 下面是输出路径看看~          {              if ( top > 0 )              {                  printf("(%d,%d)-->", path_stack[top]->s_x, path_stack[top--]->s_y);              }              else              {                  printf("(%d,%d)", path_stack[top]->s_x, path_stack[top--]->s_y);              }          }      }      else      {          printf("么有找到路径");      }     // puts("");      return 0;  }  
0 0