翻了翻别人写的博客,我看到一个A星算法,只怪自己见识太少,竟然没听过这个算法。网上查了好些资料,自己对这算法理解了些,并用C#实现出来。

A星算法,也叫A*算法。这是一种在图形平面上,有多个节点的路径,求出最低通过成本的算法。 如在一张dota地图上,英雄从一个地方走动到地图上另一个点,它选择最优路线的算法。

如上图,绿点是开始点,红点是目的地,黑色区域是不可通过区域。 通过A*算法,黄色线段就是找到的最优路线。

我想了想,其实用漫水算法也能找这路线啊。这A星算法优点在于处理速度快,并不是像漫水一样,各个方向都在寻找。

A*算法原理。就以上面那种情况说明吧, 从绿色点开始,你想去红色点,你下一步有八个方向选择。

八个方向选择,你肯定想往右边走,这样才能离目的地近。横竖走,还是斜着走,更倾向横竖走,距离短。

用G表示你从起始点走的距离。横或竖一格G=10,斜着G=14。

用H表示你离目的地的距离,这里就是个估算,就像你老远看下,在那个方向,估算下距离。用曼哈顿距离表示就可以了。 比如你在(1,1),要去(5,5).距离就当你竖着走4格,横着走4个,八格就到了。估算,别管障碍物。八格那么,H=80.变大十倍(因为G变大10倍了)。

那么用F=G+H表示你周围的格子,你要去目的地花的代价。 越小越好。

然后你建两个表,存放一些东西。你要探索的格子存在OpenList里去,你找到最好的存到CloseList去。你存的不仅仅是这个格子的F值,还有它指向的方向,不然你的G有什么意义。

然后你就有F的值了,你周围的八个格子F都有了,加入到你的OpenList里去。选个F最小的格子,移出OpenList,存到CloseList去。走到这个格子,探索这个格子周围的格子忽略障碍物和边界,忽略CloseList里的格子,其他统统加入到Openlist里。如果有格子在OpenList里,看看他们原来的G值和你走过去的G值大小比较下,如果你的G大,就让那个格子以前那样,你的G小,更新它。

然后在OpenList找最小F那个格子,移出OpenList,加入CloseList,走到这个格子,打开周围格子,循环下去,直到你哪天一不小心打开了目的地的格子。就停止吧。

最后,从目的地这个格子的指向,一路指向你开始的格子!

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace MyAXing
{class MyPoint      //定义了MyPoint这种数据结构  就是我说的上面那种格子{public int x;public int y;public int G;public int H;public MyPoint father;public MyPoint(){}public MyPoint(int x0, int y0, int G0, int H0,MyPoint F){x = x0;y = y0;G = G0;H = H0;father = F;}}
}

按照上面的思路,写出来就是了。定义两个List<MyPoint>来存Mypoint。

        List<MyPoint> Open_List = new List<MyPoint>();List<MyPoint> Close_List = new List<MyPoint>();

更新下,贴上源码//

Form1

  public partial class Form1 : Form{enum mycc{wall,start,des}mycc mychoose = mycc.wall;byte[,] R = new byte[10, 10] { { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },{ 1, 1, 1, 1, 0, 1, 1, 1, 1, 1 },{ 1, 1, 1, 1, 0, 1, 1, 1, 1, 1 },{ 1, 1, 1, 1, 0, 1, 1, 1, 1, 1 },{ 1, 1, 1, 1, 0, 1, 1, 1, 1, 1 },{ 1, 1, 1, 1, 0, 1, 1, 1, 1, 1 },{ 1, 1, 1, 1, 0, 1, 1, 1, 1, 1 },{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }};mybutton[,] mybut = new mybutton[20, 20];byte[,] myR = new byte[20, 20];MyPoint pa = new MyPoint();MyPoint pb = new MyPoint();void init(){for (int i = 0; i < 20; i++){for (int j = 0; j < 20; j++){myR[i, j] = 1;mybut[i, j].BackColor = Color.Transparent;}}}public Form1(){InitializeComponent();}private void button1_Click(object sender, EventArgs e){AxingTest ax = new AxingTest(R);//定义起始位置MyPoint pa = new MyPoint();pa.x = 1;pa.y = 1;//定义目的地MyPoint pb = new MyPoint();pb.x = 8;pb.y = 8;List<MyPoint> myp = new List<MyPoint>();myp= ax.FindeWay(pa, pb);foreach (MyPoint p in myp){//   textBox1.AppendText(p.ToString() + " ");}}private void Form1_Load(object sender, EventArgs e){for (int i = 0; i < 20; i++){for (int j = 0; j < 20; j++){mybut[i, j] = new mybutton();mybut[i, j].X = i;mybut[i, j].Y = j;mybut[i, j].Size = new Size(20, 20);mybut[i, j].Location = new Point(i * 20, j * 20);this.Controls.Add(mybut[i, j]);mybut[i, j].MouseDown += Form1_MouseDown;}}}void Form1_MouseDown(object sender, MouseEventArgs e){mybutton myb = (mybutton)sender;int x = myb.X;int y = myb.Y;if (mychoose == mycc.wall){mybut[x, y].BackColor = Color.Black;myR[y, x] = 0;}if (mychoose == mycc.start){mybut[x, y].BackColor = Color.Green;//  myR[x, y] = 1;pa.x = x;pa.y = y;}if (mychoose == mycc.des){mybut[x, y].BackColor = Color.Red;//  myR[x, y] = 1;pb.x = x;pb.y = y;}}private void button6_Click(object sender, EventArgs e){init();}private void button2_Click(object sender, EventArgs e){mychoose = mycc.wall;}private void button3_Click(object sender, EventArgs e){mychoose = mycc.start;}private void button4_Click(object sender, EventArgs e){mychoose = mycc.des;}private void button5_Click(object sender, EventArgs e){AxingTest ax = new AxingTest(myR);List<MyPoint> myp = new List<MyPoint>();myp = ax.FindeWay(pa, pb);foreach (MyPoint p in myp){mybut[p.x, p.y].BackColor = Color.Yellow;}mybut[pb.x, pb.y].BackColor = Color.Red;}}public class mybutton : Button{int x;int y;public int X{set { x = value; }get { return x; }}public int Y{set { y = value; }get { return y; }}
    }

AXingTest

    class AxingTest{List<MyPoint> Open_List = new List<MyPoint>();List<MyPoint> Close_List = new List<MyPoint>();byte[,] R;//byte[,] R = new byte[10, 10] { //    { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },//    { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },//    { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },//    { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },//    { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },//    { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },//    { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },//    { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },//    { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },//    { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }//    };public AxingTest(byte[,] R){this.R = R;}//从开启列表查找F值最小的节点private MyPoint GetMinFFromOpenList(){MyPoint Pmin = null;foreach (MyPoint p in Open_List)if (Pmin == null || Pmin.G + Pmin.H > p.G + p.H)Pmin = p;return Pmin;}//判断一个点是否为障碍物private bool IsBar(MyPoint p, byte[,] map){if (map[p.y, p.x] == 0) return true;return false;}//判断关闭列表是否包含一个坐标的点private bool IsInCloseList(int x, int y){foreach (MyPoint p in Close_List)if (p.x == x && p.y == y)return true;return false;}//从关闭列表返回对应坐标的点private MyPoint GetPointFromCloseList(int x, int y){foreach (MyPoint p in Close_List)if (p.x == x && p.y == y)return p;return null;}private bool IsInOpenList(int x, int y){foreach (MyPoint p in Open_List)if (p.x == x && p.y == y)return true;return false;}private MyPoint GetPointFromeOpenList(int x, int y){foreach (MyPoint p in Open_List)if (p.x == x && p.y == y)return p;return null;}private int GetG(MyPoint p){if (p.father == null) return 0;if (p.x == p.father.x || p.y == p.father.y)return p.father.G + 10;elsereturn p.father.G + 14;}private int GetH(MyPoint p, MyPoint pb){return Math.Abs(p.x - pb.x)*10 + Math.Abs(p.y - pb.y)*10;}private void CheckP8(MyPoint p0, byte[,] map, MyPoint pa, ref MyPoint pb){for (int xt = p0.x - 1; xt <= p0.x + 1; xt++){for (int yt = p0.y - 1; yt <= p0.y + 1; yt++){//排除超过边界和关闭自身的点if((xt>=0&&xt<R.GetLength(0)&&yt>=0&&yt<R.GetLength(1))&&!(xt==p0.x&&yt==p0.y)){//排除障碍点和关闭列表中的点if(map[yt,xt]!=0&&!IsInCloseList(xt,yt)){if (IsInOpenList(xt, yt)){MyPoint pt = GetPointFromeOpenList(xt, yt);int G_new = 0;if (p0.x == pt.x || p0.y == pt.y)G_new = p0.G + 10;elseG_new = p0.G + 14;if (G_new < pt.G){Open_List.Remove(pt);pt.father = p0;pt.G = G_new;Open_List.Add(pt);}}else{//不在开启列表中MyPoint pt = new MyPoint();pt.x = xt;pt.y = yt;pt.father = p0;pt.G = GetG(pt);pt.H = GetH(pt, pb);Open_List.Add(pt);}}}}}}public List<MyPoint> FindeWay(MyPoint pa, MyPoint pb){List<MyPoint> myp = new List<MyPoint>();Open_List.Add(pa);while (!(IsInOpenList(pb.x, pb.y) || Open_List.Count == 0)){MyPoint p0 = GetMinFFromOpenList();if (p0 == null) return null;Open_List.Remove(p0);Close_List.Add(p0);CheckP8(p0, R, pa, ref pb);}MyPoint p = GetPointFromeOpenList(pb.x, pb.y);while (p.father != null){myp.Add(p);p = p.father;// R[p.y, p.x] = 3;}return myp;}}

MyPoint

    class MyPoint{public int x;public int y;public int G;public int H;public MyPoint father;public MyPoint(){}public MyPoint(int x0, int y0, int G0, int H0,MyPoint F){x = x0;y = y0;G = G0;H = H0;father = F;}public override string ToString(){return "{"+x.ToString() + "," + y.ToString()+"}";}}

A星算法的理解和C#实现相关推荐

  1. A星算法(Java实现)

    2019独角兽企业重金招聘Python工程师标准>>> 一.适用场景 在一张地图中,绘制从起点移动到终点的最优路径,地图中会有障碍物,必须绕开障碍物. 二.算法思路 1. 回溯法得到 ...

  2. cocos2d-x游戏实例(5)-A星算法(1)

    小满(bill man)个人原创,欢迎转载,转载请注明地址,小满(bill man)的专栏地址http://blog.csdn.net/bill_man 继续上一篇地图上的处理,不过和本篇相比,我们之 ...

  3. 地图信息,障碍判断以及寻路算法(A星算法,B星算法和蚁群算法等)

    一.广度优先遍历和深度优先遍历 在学习寻路算法之前,我们先来了解一下广度优先遍历和深度优先遍历. 什么是广度优先遍历? 广度优先遍历(breadth first search)是一个万能的算法. 广度 ...

  4. 【无人机】基于A星算法实现三维栅格地图路径规划matlab代码

    1 算法介绍 A*搜寻算法俗称A星算法.这是一种在图形平面上,有多个节点的路径,求出最低通过成本的算法.常用于游戏中的NPC的移动计算,或线上游戏的BOT的移动计算上.(拷自百度百科)是常用搜索算法中 ...

  5. 不到一百行python代码简单实现A星算法

    为了更好地理解A星算法,自己手撸了一段91行的代码来实现A星算法 可能代码风格不是很好,因为这也就是一上午写出来的,只是简单实现了A星 过两天准备好好改动一下代码使其更易读,再好好备注一下. #pyt ...

  6. matlab温度数据怎么滤波_卡尔曼滤波算法思想理解 Kalman filter 第一篇

    卡尔曼滤波算法思想理解 Kalman filter 第一篇 最近在初步的理解目标跟踪的领域, 其中一个非常经典的算法卡尔曼滤波Kalman filter是需要有很好的理解才行, 由于已经脱离了学校,懂 ...

  7. cocos2d-x游戏实例(8)-A星算法(4)

    小满(bill man)个人原创,欢迎转载,转载请注明地址,小满(bill man)的专栏地址http://blog.csdn.net/bill_man 继续A星算法,我们在经历了地图的检测,并且检测 ...

  8. Interview:算法岗位面试—11.15下午上海某航天***公司(国企)技术面之工业机器视觉认知、计算机视觉算法的理解、目标检测相关项目案例

    ML岗位面试:11.15下午上海某航天***公司(国企)技术面之工业机器视觉认知.计算机视觉算法的理解.目标检测相关项目案例 Interview:算法岗位面试-11.15下午上海某航天***公司(国企 ...

  9. 北斗导航 | Select Satellite 选星算法

    ================================================ 博主github:https://github.com/MichaelBeechan 博主CSDN:h ...

  10. cocos2d-x游戏实例(9)-A星算法(5)

    小满(bill man)个人原创,欢迎转载,转载请注明地址,小满(bill man)的专栏地址http://blog.csdn.net/bill_man 上一篇我们已经完成了A星算法,那么如何使用呢, ...

最新文章

  1. java客服系统_阿里Java内部资料:2020最全Java技术栈(架构篇+算法篇+大数据)
  2. ubuntu上建立mini2440 qt编译环境
  3. 【bzoj 4764】弹飞大爷
  4. 《ArcGIS Engine 地理信息系统开发从入门到精通(第二版)》——6.7 本章小结
  5. macos 全局快捷键 打开 iterm_在 macOS 上实用的十大软件!你get了吗?
  6. 【超值干货】10个案例告诉你,数据如何驱动产品设计
  7. magento php mysql,安装lnmp nginx php mysql环境 -magento
  8. 上传本地文件到gitlab 项目里的某个文件夹
  9. Linux下使用fstatfs/statfs查询系统相关信息
  10. android 加载大长图,android加载长图片的方法
  11. weight_decay一般设置为多少_50岁的夫妻一般有多少存款?他们在为养老做准备吗?...
  12. php sesssion,php sesssion原理
  13. markdown字体颜色_Markdown转微信公众号格式
  14. 股票投资的基本面量化
  15. python程序设计题库-知到智慧树_Python程序设计基础_完整免费答案
  16. Windows蓝屏自动修复无法修复你的电脑
  17. 蓝桥杯 历年试题 矩阵翻硬币
  18. 0CTF piapiapia
  19. 单链表的逆置算法解析
  20. pygame制作游戏全套的

热门文章

  1. 图像拼接算法(zz)
  2. 蜂鸣器发声程序c语言,基于51单片机蜂鸣器发声的C语言程序
  3. 整数规划遗传算法MATLAB,非线性整数规划的遗传算法Matlab程序
  4. 自己动手做一个爬虫项目
  5. IT之路,从迷茫“愤青”到团队项目经理,他是如何一步步走出来的?
  6. iPhone配置实用工具
  7. 【GlobalMapper精品教程】001:GlobalMapper23 Pro-x64中文安装教程(附软件包下载)
  8. 用mysql做宠物商店项目_使用Java实现数据库编程 项目(宠物商店)
  9. oracle 11g varchar/varchar2
  10. VMware及相关产品序列号大全