A*

A*(A-Star)算法是一种静态路网中求解最短路最有效的直接搜索方法。
注意是最有效的直接搜索算法。之后涌现了很多预处理算法(ALT,CH,HL等等),在线查询效率是A*算法的数千甚至上万倍。
公式表示为: f(n)=g(n)+h(n)
其中 f(n) 是从初始点经由节点n到目标点的估价函数,
g(n) 是在状态空间中从初始节点到n节点的实际代价,
h(n) 是从n到目标节点最佳路径的估计代价。
保证找到最短路径(最优解的)条件,关键在于估价函数f(n)的选取:
估价值h(n)<= n到目标节点的距离实际值,这种情况下,搜索的点数多,搜索范围大,效率低。但能得到最优解。并且如果h(n)=d(n),即距离估计h(n)等于最短距离,那么搜索将严格沿着最短路径进行, 此时的搜索效率是最高的。
如果 估价值>实际值,搜索的点数少,搜索范围小,效率高,但不能保证得到最优解。



[html]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. using UnityEngine;  
  2. using System.Collections;  
  3. using System.Collections.Generic;  
  4. using System;  
  5.   
  6. /// <summary>  
  7. /// 格子类型  
  8. /// </summary>  
  9. public enum GridType  
  10. {  
  11.     Normal,  
  12.     Obstacle,  
  13.     Start,  
  14.     End  
  15. }  
  16. /// <summary>  
  17. /// 为了实现格子的排序,继承ICompareable  
  18. /// </summary>  
  19. public class MapGrid:IComparable  
  20. {  
  21.     /// <summary>  
  22.     /// 记录坐标  
  23.     /// </summary>  
  24.     public int x;  
  25.     public int y;  
  26.   
  27.     /// <summary>  
  28.     /// 总消耗  
  29.     /// </summary>  
  30.     public int f;  
  31.     /// <summary>  
  32.     /// 当前到起点的消耗  
  33.     /// </summary>  
  34.     public int g;  
  35.     //当前到终点的消耗  
  36.     public int h;  
  37.   
  38.     public GridType type;  
  39.     /// <summary>  
  40.     /// 父节点  
  41.     /// </summary>  
  42.     public MapGrid fatherNode;  
  43.   
  44.     /// <summary>  
  45.     /// 排序  
  46.     /// </summary>  
  47.     /// <param name="obj"></param>  
  48.     /// <returns></returns>  
  49.     public int CompareTo(object obj)  
  50.     {  
  51.         MapGrid grid = (MapGrid)obj;  
  52.         if(this .f <grid .f)  
  53.         {  
  54.             //升序  
  55.             return -1;  
  56.         }  
  57.         if(this .f >grid .f)  
  58.         {  
  59.             //降序  
  60.             return 1;  
  61.         }  
  62.         return 0;  
  63.     }  
  64. }  
  65.   
  66.   
  67. public class AStar : MonoBehaviour {  
  68.       
  69.     public int row = 5;  
  70.     public int col = 10;  
  71.     //格子大小  
  72.     public int size = 70;  
  73.   
  74.     public MapGrid[,] grids;  
  75.     public ArrayList openList;  
  76.     public ArrayList closeList;  
  77.   
  78.     //开始和结束的位置  
  79.     private int xStart = 2;  
  80.     private int yStart = 1;  
  81.     private int xEnd = 2;  
  82.     private int yEnd = 5;  
  83.   
  84.     private Stack<string> fatherNodeLocation;  
  85.   
  86.     void Init()  
  87.     {  
  88.         grids = new MapGrid[row, col];  
  89.   
  90.         for (int i = 0; i < row; i++)  
  91.         {  
  92.             for (int j = 0; j < col; j++)  
  93.             {  
  94.                 //初始化格子,记录格子的坐标  
  95.                 grids[i, j] = new MapGrid();  
  96.                 grids[i, j].x = i;  
  97.                 grids[i, j].y = j;  
  98.             }  
  99.         }  
  100.         //开始点和其h的值  
  101.         grids[xStart, yStart].type = GridType.Start;  
  102.         grids[xStart, yStart].h = Manhatten(xStart, yStart);  
  103.   
  104.         grids[xEnd, yEnd].type = GridType.End;  
  105.   
  106.         fatherNodeLocation = new Stack<string>();  
  107.   
  108.         for (int i = 1; i <= 3; i++)  
  109.         {  
  110.             grids[i, 3].type = GridType.Obstacle;  
  111.         }  
  112.   
  113.         openList = new ArrayList();  
  114.         openList.Add(grids[xStart, yStart]);  
  115.   
  116.         closeList = new ArrayList();  
  117.     }  
  118.   
  119.     private int Manhatten(int x,int y)  
  120.     {  
  121.         return (int)(Math.Abs(xEnd - x) + Math.Abs(yEnd - y)) * 10;  
  122.     }  
  123.   
  124.     void Start()  
  125.     {  
  126.         Init();  
  127.     }  
  128.   
  129.     void DrawGrid()  
  130.     {  
  131.         for (int i = 0; i < row ; i++)  
  132.         {  
  133.             for (int j = 0; j < col ; j++)  
  134.             {  
  135.                 Color color = Color.yellow;  
  136.                 if(grids [i,j].type ==GridType .Start )  
  137.                 {  
  138.                     color = Color.green;  
  139.                 }  
  140.                 else if(grids [i,j].type ==GridType .Start )  
  141.                 {  
  142.                     color = Color.red;  
  143.                 }  
  144.                 else if (grids [i,j].type ==GridType .Obstacle )  
  145.                 {  
  146.                     color = Color.blue;  
  147.                 }  
  148.                 else if(closeList .Contains (grids [i,j]))  
  149.                 {  
  150.                     color = Color.yellow;  
  151.                 }  
  152.                 else  
  153.                 {  
  154.                     color = Color.grey;  
  155.                 }  
  156.   
  157.                 GUI.backgroundColor = color;  
  158.                 GUI.Button(new Rect(j * size, i * size, size, size), FGH(grids[i, j]));  
  159.             }  
  160.         }  
  161.     }  
  162.   
  163.     private string FGH(MapGrid grid)  
  164.     {  
  165.         string str = "F" + grid.f + "\n";  
  166.         str += "G" + grid.g + "\n";  
  167.         str += "H" + grid.h + "\n";  
  168.         str += "(" + grid.x + "," + grid.y + ")";  
  169.         return str;  
  170.     }  
  171.   
  172.     void OnGUI()  
  173.     {  
  174.         DrawGrid();  
  175.         for (int i = 0; i < openList .Count ; i++)  
  176.         {  
  177.             GUI.Button(new Rect(i * size, (row + 1) * size, size, size), FGH((MapGrid)openList[i]));  
  178.         }  
  179.   
  180.         for (int j = 0; j < closeList .Count ; j++)  
  181.         {  
  182.             GUI.Button(new Rect(j * size, (row + 1) * size, size, size), FGH((MapGrid)closeList[j]));  
  183.         }  
  184.   
  185.         if(GUI .Button (new Rect (col *size ,size ,size ,size ),"next"))  
  186.         {  
  187.             NextStep();  
  188.         }  
  189.     }  
  190.   
  191.     private void NextStep()  
  192.     {  
  193.         if (openList.Count == 0)  
  194.         {  
  195.             print("over !");  
  196.             return;  
  197.         }  
  198.   
  199.         MapGrid grid=(MapGrid )openList [0];  
  200.         if(grid.type ==GridType .End )  
  201.         {  
  202.             print ("find");  
  203.             ShowFatherNode(grid);  
  204.             return ;  
  205.         }  
  206.   
  207.         for (int i = -1; i <=1; i++)  
  208.         {  
  209.             for (int j = -1; j <=1; j++)  
  210.             {  
  211.                 int x = grid.x + i;  
  212.                 int y = grid.y + j;  
  213.   
  214.                 if(x>=0&&x<row &&y>=0&&y<col&&  
  215.                     grids [x,y].type !=GridType .Obstacle &&  
  216.                     !closeList .Contains (grids[x,y]))  
  217.                 {  
  218.                     int g = grid.g + (int)(Math.Sqrt((Math.Abs(i) + Math.Abs(j))) * 10);  
  219.                     if(grids[x,y].g==0||grids [x,y].g>g)  
  220.                     {  
  221.                         grids[x, y].g = g;  
  222.                         grids[x, y].fatherNode = grid;  
  223.                     }  
  224.   
  225.                     grids[x, y].h = Manhatten(x, y);  
  226.                     grids[x, y].f = grids[x, y].g + grids[x, y].h;  
  227.   
  228.                     if(!openList .Contains (grids[x,y]))  
  229.                     {  
  230.                         openList.Add(grids[x, y]);  
  231.                     }  
  232.   
  233.                     openList.Sort();  
  234.                 }  
  235.             }  
  236.         }  
  237.   
  238.         closeList.Add(grid);  
  239.         openList.Remove(grid);  
  240.     }  
  241.   
  242.     private void ShowFatherNode(MapGrid grid)  
  243.     {  
  244.         if(grid.fatherNode !=null )  
  245.         {  
  246.             string str = grid.fatherNode.x + "," + grid.fatherNode.y;  
  247.             fatherNodeLocation.Push(str);  
  248.             ShowFatherNode(grid.fatherNode);  
  249.         }  
  250.   
  251.         if(fatherNodeLocation .Count !=0)  
  252.         {  
  253.             print(fatherNodeLocation.Pop());  
  254.         }  
  255.     }  
  256. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值