回顾A*算法,偶得一源代码,略有瑕疵,改正之,并置于下。

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace AStarOne
{
class AStar
{
public const int OBLIQUE = ;//sqrt(2.0) is 1.414; they have been amplified.
public const int STEP = ;
public int[,] AStarArray { get; private set; }
List<Point> CloseList; // Close List
List<Point> OpenList; // Open List public AStar(int [,] aStar)
{
this.AStarArray = aStar;
OpenList = new List<Point>(AStarArray.Length);// Impossible to be bigger than the number of all data
CloseList = new List<Point>(AStarArray.Length);
} /// <summary>
/// Find an achievable path from start to end
/// </summary>
/// <param name="start"></param>
/// <param name="end"></param>
/// <param name="IsIgnoreCorner">If ignore diagonal spot</param>
/// <returns></returns>
public Point FindPath(Point start, Point end, bool IsIgnoreCorner)
{
OpenList.Add(start);
while ( != OpenList.Count)
{
var tempStart = OpenList.MinPoint();// get the minimum F
OpenList.RemoveAt();
CloseList.Add(tempStart); var surroundPoints = GetSurroundPoints(tempStart, IsIgnoreCorner);// Get surrounding points
foreach (var point in surroundPoints)
{
if (OpenList.Exists(point))// If existing in the open list, choose the minimum G between tempStart and point; Update
{
FoundPoint(tempStart, point);
}
else
{
NotFoundPoint(tempStart, end, point);
}
} if (OpenList.Get(end) != null)
return OpenList.Get(end);
} return OpenList.Get(end);
} private void FoundPoint(Point tempStart, Point point)
{
var G = CalcG(tempStart, point);
if (G < point.G)// the minimum one
{
point.ParentPoint = tempStart;
point.G = G;
point.CalcF();
}
} private void NotFoundPoint(Point tempPoint, Point end, Point point)
{
point.ParentPoint = tempPoint;
point.G = CalcG(tempPoint, point);
point.H = CalcH(end, point);
point.CalcF();
OpenList.Add(point);// This is quite important
} private int CalcG(Point start, Point point)// Calc the cost from start to point
{
int G = (Math.Abs(point.X - start.X) + Math.Abs(point.Y - start.Y)) == ? STEP : OBLIQUE;// Should be 1
int parentG = point.ParentPoint != null ? point.ParentPoint.G : ;
return G + parentG;
} private int CalcH(Point end, Point point)// Estimate the cost to reach the target
{
int step = Math.Abs(point.X - end.X) + Math.Abs(point.Y - end.Y);
return step * STEP;
} public List<Point> GetSurroundPoints(Point point, bool IsIgnoreCorner)
{
var surroundPoints = new List<Point>(); for (int x = point.X - ; x <= point.X + ; x++)
{
for (int y = point.Y - ; y <= point.Y + ; y++)
{
if (CanReach(point, x, y, IsIgnoreCorner))
surroundPoints.Add(x, y);
}
} return surroundPoints;
} private bool CanReach(int x, int y)
{
return AStarArray[x, y] == ;
} public bool CanReach(Point start, int x, int y, bool IsIgnoreCorner)
{
if (!CanReach(x, y) || CloseList.Exists(x, y))// Cannot reach or has been handled
{
return false;
}
else
{
if (Math.Abs(x - start.X) + Math.Abs(y - start.Y) == )// Adjacent but not diagonal
{
return true;
}
else
{
if (CanReach(Math.Abs(x - ), y) && CanReach(x, Math.Abs(y - )))// Make sure diagnonal but not necessary
{
return IsIgnoreCorner;
}
else
{
return false;
}
}
}
}
} public class Point
{
public Point ParentPoint { get; set; }
public int F { get; set; } // F = G + H
public int G { get; set; }
public int H { get; set; }
public int X { get; set; }
public int Y { get; set; } public Point(int x, int y)
{
this.X = x;
this.Y = y;
} public void CalcF()
{
this.F = this.G + this.H;
}
} public static class ListHelper
{
public static bool Exists(this List<Point> points, Point point)
{
foreach (var p in points)
if ((p.X == point.X) && (p.Y == point.Y))
return true; return false;
} public static bool Exists(this List<Point> points, int x, int y)
{
foreach (var p in points)
if ((p.X == x) && (p.Y == y))
return true; return false;
} public static Point MinPoint(this List<Point> points)
{
points = points.OrderBy(p => p.F).ToList();
return points[];
} public static void Add(this List<Point> points, int x, int y)
{
points.Add(new Point(x, y));
} public static Point Get(this List<Point> points, Point point)
{
foreach (Point p in points)
if ((p.X == point.X) && (p.Y == point.Y))
return p; return null;
} public static void Remove(this List<Point> points, int x, int y)
{
foreach (var point in points)
if ((point.X == x) && (point.Y == y))
points.Remove(point);
}
}
}

测试代码如下:

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace AStarOne
{
class Program
{
static void Main(string[] args)
{
int[,] array = {
{ , , , , , , , , , , , },
{ , , , , , , , , , , , },
{ , , , , , , , , , , , },
{ , , , , , , , , , , , },
{ , , , , , , , , , , , },
{ , , , , , , , , , , , },
{ , , , , , , , , , , , },
{ , , , , , , , , , , , }
}; AStar astar = new AStar(array); Point start = new Point(, );
Point end = new Point(, );
var parent = astar.FindPath(start, end, false); Console.WriteLine("Print path:");
while (parent != null)
{
//Console.WriteLine(parent.X + ", " + parent.Y);
array[parent.X, parent.Y] = ;
parent = parent.ParentPoint;
} for (int i = ; i < ; i++)
{
for (int j = ; j < ; j++)
{
Console.Write(array[i,j] + " ");
}
Console.WriteLine();
}
}
}
}

运行结果如下(注意‘8’的位置即是路径):

A Star算法笔记的更多相关文章

  1. 学习Java 以及对几大基本排序算法(对算法笔记书的研究)的一些学习总结(Java对算法的实现持续更新中)

    Java排序一,冒泡排序! 刚刚开始学习Java,但是比较有兴趣研究算法.最近看了一本算法笔记,刚开始只是打算随便看看,但是发现这本书非常不错,尤其是对排序算法,以及哈希函数的一些解释,让我非常的感兴 ...

  2. 算法笔记--数位dp

    算法笔记 这个博客写的不错:http://blog.csdn.net/wust_zzwh/article/details/52100392 数位dp的精髓是不同情况下sta变量的设置. 模板: ]; ...

  3. 算法笔记--lca倍增算法

    算法笔记 模板: vector<int>g[N]; vector<int>edge[N]; ][N]; int deep[N]; int h[N]; void dfs(int ...

  4. 算法笔记--STL中的各种遍历及查找(待增)

    算法笔记 map: map<string,int> m; map<string,int>::iterator it;//auto it it = m.begin(); whil ...

  5. 算法笔记--priority_queue

    算法笔记 priority_queue<int>que;//默认大顶堆 或者写作:priority_queue<int,vector<int>,less<int&g ...

  6. 算法笔记--sg函数详解及其模板

    算法笔记 参考资料:https://wenku.baidu.com/view/25540742a8956bec0975e3a8.html sg函数大神详解:http://blog.csdn.net/l ...

  7. 算法笔记——C/C++语言基础篇(已完结)

    开始系统学习算法,希望自己能够坚持下去,期间会把常用到的算法写进此博客,便于以后复习,同时希望能够给初学者提供一定的帮助,手敲难免存在错误,欢迎评论指正,共同学习.博客也可能会引用别人写的代码,如有引 ...

  8. 算法笔记_067:蓝桥杯练习 算法训练 安慰奶牛(Java)

    目录 1 问题描述 2 解决方案   1 问题描述 问题描述 Farmer John变得非常懒,他不想再继续维护供奶牛之间供通行的道路.道路被用来连接N个牧场,牧场被连续地编号为1到N.每一个牧场都是 ...

  9. 算法笔记(c++)--回文

    算法笔记(c++)--回文 #include<iostream> #include<algorithm> #include<vector> using namesp ...

随机推荐

  1. PHP开发框架Laravel优点,Laravel5.3中文文档

    PHP开发框架Laravel优点 Laravel的设计思想是很先进的,非常适合应用各种开发模式TDD, DDD和BDD,作为一个框架,它为你准备好了一切,composer是个php的未来,没有comp ...

  2. Asp.Net Mvc视图引擎Razor介绍

    1.Razor介绍 1)ASP.NET MVC3 带来了一种新的名为Razor 的视图引擎,提供了下列优点: Razor 的语法简单且清晰,只需要最小化的输入 Razor 容易学习,语法类似于 C#  ...

  3. 那些情况该使用它们spin_lock到spin_lock_irqsave【转】

    转自:http://blog.csdn.net/wesleyluo/article/details/8807919 权声明:本文为博主原创文章,未经博主允许不得转载. Spinlock的目的是用来同步 ...

  4. NEON简介【转】

    转自:http://blog.csdn.net/fengbingchun/article/details/38020265 版权声明:本文为博主原创文章,未经博主允许不得转载. “ARM Advanc ...

  5. 实现Ecshop商品跳到淘宝、京东等的购买链接

    今天简单的实现了一下ecshop商品导出到第三方的购买链接功能.大致思路是给商品添加一个buy_link的text字段,存为json结构,然后通过json解析输出到商品购买页面 1.添加字段 增加购买 ...

  6. word 排版问题

    1.wps word为何设置了页边距后下面的页边距不变呢 在章节选项卡中,查看章节导航,有可能是文档分节了,光标所在的节已经调整,而你看到页是另一节 2.分栏 选中你要进行分栏的内容,进行分栏,也可以 ...

  7. android 动画总结

    以下博文讲解比较详细,可查阅: http://www.360doc.com/content/13/0102/22/6541311_257754535.shtml 几个关键属性: setRepeatCo ...

  8. hack是什么

    不同浏览器对css的解析是不同是,因此需要css hack来解决浏览器局部的兼容性问题.针对不同浏览器写不同的CSS代码的过程叫CSS  Hack. 常见的hack有三种形式,分别是CSS属性hack ...

  9. Linux学习之八——利用变量

    一.变量的使用 用$放在变量前面进行使用,例如: echo $PATH 为了和别的字符隔开,可以用{}和"",例如 echo ${PATH}nic echo "$PATH ...

  10. select resharper shortcuts scheme

    VS代码生成工具ReSharper提供了丰富的快捷键,可以极大地提高你的开发效率.安装ReSharper后首次启动Visual Studio时,会出现一个名为ReSharper Keyboard Sc ...