您好,欢迎光临本网站![请登录][注册会员]  
文件名称: A*算法 C#源代码
  所属分类: 其它
  开发工具:
  文件大小: 326kb
  下载次数: 0
  上传时间: 2009-12-23
  提 供 者: ysg8*****
 详细说明: 此为用C#写的A*算法源代码 using System; using System.Collections.Generic; using System.Text; using System.Drawing; namespace EtSoft.AStarLib { public class AStar { private StarNodeCollection openList = new StarNodeCollection(); private StarNodeCollection closeList = new StarNodeCollection(); public StarNode currentNode = null; #region 构造函数 /// /// 使用指定的地图对象、起点和终点初始化A星算法 /// /// 地图对象 public AStar(Map map) { this.map = map; } /// /// /// /// 地图对象 /// 起点坐标 /// 终点坐标 public AStar(Map map, Point start, Point end) : this(map) { this.start = new StarNode(start); this.end = new StarNode(end); openList.Add(new StarNode(start)); //AddStartNodeToOpenList(this.start); } /// /// /// /// 地图对象 /// 起点X坐标 /// 起点Y坐标 /// 终点X坐标 /// 终点Y坐标 public AStar(Map map, int startX, int startY, int endX, int endY) : this(map, new Point(startX, startY), new Point(endX, endY)) { } #endregion #region 属性 protected Map map; /// /// 地图数据 /// public Map Map { get { return map; } set { map = value; } } private StarNode start = null; /// /// 起点坐标,如果没有设置起点,返回null /// public StarNode Start { get { return start; } set { start = value; openList.Clear(); openList.Add(start); //AddNodeToOpenList(start); } } private StarNode end = null; /// /// 终点坐标,如果没有设置终点,返回null /// public StarNode End { get { return end; } set { end = value; } } private StarNodeCollection path; /// /// 返回路径节点集合,没有路径则返回null /// public StarNodeCollection Path { get { return path; } } private bool allDirection = false; /// /// true,允许八方向寻路 /// public bool AllDirection { get { return allDirection; } set { allDirection = value; } } #endregion /// /// 开始寻路 /// public void StartSearchPath() { if (start == null) throw new InvalidNodeException(InvalidNodeTypes.NoStart); if (end == null) throw new InvalidNodeException(InvalidNodeTypes.NoEnd); path = null; openList.Clear(); closeList.Clear(); currentNode = start; SearchPath(currentNode); } //寻路递归,受保护的虚方法,允许在子类重写寻路算法 protected virtual void SearchPath(StarNode starNode) { //currentNode = starNode; openList.Remove(starNode); closeList.Add(starNode); AddNodeToOpenList(); if (closeList.Contains(end)) { //如果终点在关闭列表中,找到路径 StarNode node=starNode.Parent; path = new StarNodeCollection(); do { path.Add(node); node = node.Parent; } while (node != null && !node.Equals(start)); path.Reverse(); return; } else if (openList.Count == 0) { //终点不在关闭列表,开放列表已空,没有可通行的路径 return; } currentNode = GetNextNode(); //迭代过程 SearchPath(currentNode); } /// /// 获得当前节点的下一个最佳节点 /// /// public StarNode GetNextNode() { openList.SortByF(); return openList[0]; } /// /// 把当前点周围可通过的点加入到开放列表中 /// ...展开收缩
(系统自动生成,下载前可以参看下载内容)

下载文件列表

相关说明

  • 本站资源为会员上传分享交流与学习,如有侵犯您的权益,请联系我们删除.
  • 本站是交换下载平台,提供交流渠道,下载内容来自于网络,除下载问题外,其它问题请自行百度
  • 本站已设置防盗链,请勿用迅雷、QQ旋风等多线程下载软件下载资源,下载后用WinRAR最新版进行解压.
  • 如果您发现内容无法下载,请稍后再次尝试;或者到消费记录里找到下载记录反馈给我们.
  • 下载后发现下载的内容跟说明不相乎,请到消费记录里找到下载记录反馈给我们,经确认后退回积分.
  • 如下载前有疑问,可以通过点击"提供者"的名字,查看对方的联系方式,联系对方咨询.
 相关搜索: A*算法
 输入关键字,在本站1000多万海量源码库中尽情搜索: