整理硬盘的时候,发现我早些年写的A星寻路算法。特放上来,待有缘人拿去,无递归噢,性能那是杠杠的。

码上伺候

 public class Node
{
public int X
{
get;
set;
} public int Y
{
get;
set;
} public int G
{
get;
set;
} public int H
{
get;
set;
} public int F
{
get;
set;
} public Node parentNode
{
get;
set;
} public Node(int x, int y, Node parent)
{
this.X = x;
this.Y = y;
this.parentNode = parent;
}
}

Node

 public class AStar
{
private int[,] map;
private List<Node> openList;
private List<Node> closeList;
private const int COST_STRAIGHT = ;
private const int COST_DIAGONAL = ;
private int row;
private int column; public AStar(int[,] map, int row, int column)
{
this.map = map;
this.row = row;
this.column = column;
openList = new List<Node>();
closeList = new List<Node>();
} //查找坐标
public int Search(int x1, int y1, int x2, int y2)
{
if (x1 < || x1 >= row || x2 < || x2 >= row || y1 < || y1 >= column || y2 < || y2 >= column)
{
return -;
} if (map[x1,y1] == || map[x2,y2] == )
{
return -;
} Node start = new Node(x1, y1, null);
Node end = new Node(x2, y2, null);
openList.Add(start);
List<Node> resultList = Search(start, end);
if (resultList.Count == )
{
return ;
} foreach (Node node in resultList)
{
map[node.X,node.Y] = -;
} return ;
} private List<Node> Search(Node start, Node end)
{
List<Node> result = new List<Node>();
bool isFind = false;
Node node = null;
openList.Sort(new NodeComparator()); while (openList.Count > )
{
node = openList[];
if (node.X == end.X && node.Y == end.Y)
{
isFind = true;
break;
}
//上
if ((node.Y - ) >= )
{
CheckPath(node.X, node.Y - , node, end, COST_STRAIGHT);
}
//下
if ((node.Y + ) < column)
{
CheckPath(node.X, node.Y + , node, end, COST_STRAIGHT);
}
//左
if ((node.X - ) >= )
{
CheckPath(node.X - , node.Y, node, end, COST_STRAIGHT);
}
//右
if ((node.X + ) < row)
{
CheckPath(node.X + , node.Y, node, end, COST_STRAIGHT);
}
//左上
if ((node.X - ) >= && (node.Y - ) >= )
{
CheckPath(node.X - , node.Y - , node, end, COST_DIAGONAL);
}
//左下
if ((node.X - ) >= && (node.Y + ) < column)
{
CheckPath(node.X - , node.Y + , node, end, COST_DIAGONAL);
}
//右上
if ((node.X + ) < row && (node.Y - ) >= )
{
CheckPath(node.X + , node.Y - , node, end, COST_DIAGONAL);
}
//右下
if ((node.X + ) < row && (node.Y + ) < column)
{
CheckPath(node.X + , node.Y + , node, end, COST_DIAGONAL);
} openList.Remove(node);
closeList.Add(node);
}
if (isFind) {
GetPath(result, node);
} return result;
} private bool CheckPath(int x, int y, Node parentNode, Node end, int cost)
{
Node node = new Node(x, y, parentNode);
if (map[x,y] == )
{
closeList.Add(node);
return false;
}
if (IsListContains(closeList, x, y) != -)
{
return false;
}
int index = -;
if ((index = IsListContains(openList, x, y)) != -)
{
if ((parentNode.G + cost) < openList[index].G)
{
node.parentNode = parentNode;
CountG(node, cost);
CountF(node);
openList[index] = node;
}
}
else {
node.parentNode = parentNode;
Count(node, end, cost);
openList.Add(node);
} return true;
} private int IsListContains(List<Node> list, int x, int y)
{
int i = ;
foreach (Node node in list)
{
if (node.X == x && node.Y == y)
{
return i;
}
i += ;
} return -;
} private void GetPath(List<Node> list, Node node)
{
while (node.parentNode != null)
{
list.Add(node);
node = node.parentNode;
} list.Add(node); } private void Count(Node node, Node end, int cost)
{
CountG(node, cost);
CountH(node, end);
CountF(end);
} private void CountG(Node node, int cost)
{
if (node.parentNode == null)
{
node.G = cost;
}
else
node.G = node.parentNode.G + cost;
} private void CountH(Node node, Node end)
{
node.H = Math.Abs(node.X - end.X) + Math.Abs(node.Y - end.Y);
} private void CountF(Node node)
{
node.F = node.G + node.H;
}
}

AStar

 class Program
{
delegate void FuncDelegate(); static void Measure(FuncDelegate func, string funcName)
{
Stopwatch sw = new Stopwatch();
sw.Start();
func();
sw.Stop();
Console.WriteLine(" {0} used {1} ms", funcName, sw.ElapsedMilliseconds);
} static void DrawTheMap(int[,] map)
{
for (int x = ; x < ; x++)
{
for (int y = ; y < ; y++)
{
if (map[x, y] == )
{
Console.Write("□");
}
else if (map[x, y] == )
{
Console.Write("■");
}
else if (map[x, y] == -)
{
Console.Write("※");
}
}
Console.WriteLine("");
}
} static void AstarAlgorim()
{
int[,] map = new int[,] {
{,,,,,,,,,},
{,,,,,,,,,},
{,,,,,,,,,},
{,,,,,,,,,},
{,,,,,,,,,},
{,,,,,,,,,},
{,,,,,,,,,},
}; AStar star = new AStar(map, , );
int flag = star.Search(, , , );
if (flag == -)
{
Console.WriteLine("传输数据有误!");
}
else if (flag == )
{
Console.WriteLine("没有找到!");
}
else
{
DrawTheMap(map);
}
} static void Main(string[] args)
{
Measure(AstarAlgorim, "A星寻路"); Console.ReadLine();
}
}

Program

无递归 A星寻路算法的更多相关文章

  1. A星寻路算法

    A星寻路算法 1.准备一个close关闭列表(存放已被检索的点),一个open开启列表(存放未被检索的点),一个当前点的对象cur 2.将cur设成开始点 3.从cur起,将cur点放入close表中 ...

  2. 基于Unity的A星寻路算法(绝对简单完整版本)

    前言 在上一篇文章,介绍了网格地图的实现方式,基于该文章,我们来实现一个A星寻路的算法,最终实现的效果为: 项目源码已上传Github:AStarNavigate 在阅读本篇文章,如果你对于里面提到的 ...

  3. A星寻路算法介绍

    你是否在做一款游戏的时候想创造一些怪兽或者游戏主角,让它们移动到特定的位置,避开墙壁和障碍物呢? 如果是的话,请看这篇教程,我们会展示如何使用A星寻路算法来实现它! 在网上已经有很多篇关于A星寻路算法 ...

  4. cocos2d-x学习日志(13) --A星寻路算法demo

    你是否在做一款游戏的时候想创造一些怪兽或者游戏主角,让它们移动到特定的位置,避开墙壁和障碍物呢?如果是的话,请看这篇教程,我们会展示如何使用A星寻路算法来实现它! A星算法简介: A*搜寻算法俗称A星 ...

  5. A星寻路算法(A* Search Algorithm)

    你是否在做一款游戏的时候想创造一些怪兽或者游戏主角,让它们移动到特定的位置,避开墙壁和障碍物呢? 如果是的话,请看这篇教程,我们会展示如何使用A星寻路算法来实现它! 在网上已经有很多篇关于A星寻路算法 ...

  6. A星寻路算法入门(Unity实现)

    最近简单学习了一下A星寻路算法,来记录一下.还是个萌新,如果写的不好,请谅解.Unity版本:2018.3.2f1 A星寻路算法是什么 游戏开发中往往有这样的需求,让玩家控制的角色自动寻路到目标地点, ...

  7. A星寻路算法-Mind&Hand(C++)

    //注1:Mind & Hand,MIT校训,这里指的理解与实现(动脑也动手) //注2:博文分为两部分:(1)理解部分,为参考其他优秀博文的摘要梳理:(2)代码部分,是C++代码实现的,源码 ...

  8. 【Android】基于A星寻路算法的简单迷宫应用

    简介 基于[漫画算法-小灰的算法之旅]上的A星寻路算法,开发的一个Demo.目前实现后退.重新载入.路径提示.地图刷新等功能.没有做太多的性能优化,算是深化对A星寻路算法的理解. 界面预览: 初始化: ...

  9. [转载]A星寻路算法介绍

    转载自:http://www.raywenderlich.com/zh-hans/21503/a%E6%98%9F%E5%AF%BB%E8%B7%AF%E7%AE%97%E6%B3%95%E4%BB% ...

随机推荐

  1. STM32 CAN 波特率设置及采样点设置

    一.CAN波特率 STM32 位时间定义: ● 同步段(SYNC_SEG):通常期望位的变化发生在该时间段内.其值固定为1个时间单元(1 x tCAN).● 时间段1(BS1):定义采样点的位置.它包 ...

  2. JMeter学习资料

    JMeter User Manual: http://jmeter.apache.org/usermanual/index.html JMeter Componet reference: http:/ ...

  3. awk--动作(action)

    摘要 在awk--简述中我们讲到awk是由pattern-action组合而成的,关于pattern我们已经awk--模式(pattern)在讲述,接下来就来看下awk的action. 动作是什么 我 ...

  4. Oracle 数据库基本操作——表操作:查询

    目录: 1.基本查询 2.多表查询 3.多行查询 4.集合查询 2.连接 3.嵌套查询 1.基本查询 语法: select column|others{,columnName|others} from ...

  5. motan源码分析七:序列化

    motan的序列化支持两种协议,一种是json,另一种是hessian2.主要涉及到的类和接口是是:FastJsonSerialization.Hessian2Serialization.Serial ...

  6. SKTransition类

    继承自 NSObject 符合 NSObject(NSObject) 框架  /System/Library/Frameworks/SpriteKit.framework 可用性 可用于iOS 7.0 ...

  7. ajax_get方式

    test_ajax_get.html <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" " ...

  8. linux命令chown修改文件所有权

      Changing User Ownership To apply appropriate permissions, the first thing to consider is ownership ...

  9. 数据库 SQL 外键约束 多表查询

    多表设计与多表查询 1.外键约束        表是用来保存现实生活中的数据的,而现实生活中数据和数据之间往往具有一定的关系,我们在使用表来存储数据时,可以明确的声明表和表之前的依赖关系,命令数据库来 ...

  10. el和jstl

    <%@page import="cn.bdqn.bean.News"%> <%@ page language="java" import=&q ...