117.info
人生若只如初见

C#中Astar寻路算法怎么实现

以下是一种基本的A*寻路算法的实现示例,可以用于C#语言:

using System;
using System.Collections.Generic;
public class Node
{
public int X { get; set; }
public int Y { get; set; }
public bool IsObstacle { get; set; }
public int G { get; set; }  // G值表示起点到该节点的实际代价
public int H { get; set; }  // H值表示该节点到目标节点的估计代价
public int F { get { return G + H; } }  // F值表示总代价,F = G + H
public Node Parent { get; set; }  // 父节点,用于回溯路径
public Node(int x, int y, bool isObstacle = false)
{
X = x;
Y = y;
IsObstacle = isObstacle;
G = int.MaxValue;
H = 0;
Parent = null;
}
}
public class AStar
{
private int[,] _map;  // 地图,用二维数组表示
private int _width;  // 地图宽度
private int _height;  // 地图高度
public AStar(int[,] map)
{
_map = map;
_width = map.GetLength(0);
_height = map.GetLength(1);
}
public List FindPath(Node startNode, Node targetNode)
{
List openList = new List();  // 开放列表
List closedList = new List();  // 关闭列表
startNode.G = 0;
startNode.H = CalculateHeuristic(startNode, targetNode);
openList.Add(startNode);
while (openList.Count > 0)
{
// 从开放列表中选择F值最小的节点作为当前节点
Node currentNode = FindNodeWithLowestFScore(openList);
openList.Remove(currentNode);
closedList.Add(currentNode);
if (currentNode == targetNode)
{
// 找到路径,返回路径上的节点
return GeneratePath(startNode, targetNode);
}
List neighbors = GetNeighbors(currentNode);
foreach (Node neighbor in neighbors)
{
if (closedList.Contains(neighbor) || neighbor.IsObstacle)
{
// 跳过已在关闭列表中的节点或障碍节点
continue;
}
int tentativeG = currentNode.G + 1;  // G值暂时设为当前节点的G值加上从当前节点到相邻节点的实际代价
if (!openList.Contains(neighbor) || tentativeG < neighbor.G)
{
// 若该相邻节点不在开放列表中,或者新的G值更小,则更新G、H和父节点
neighbor.G = tentativeG;
neighbor.H = CalculateHeuristic(neighbor, targetNode);
neighbor.Parent = currentNode;
if (!openList.Contains(neighbor))
{
openList.Add(neighbor);
}
}
}
}
// 无法找到路径,返回空列表
return new List();
}
private int CalculateHeuristic(Node node, Node targetNode)
{
// 使用曼哈顿距离作为启发函数(估计代价)
return Math.Abs(node.X - targetNode.X) + Math.Abs(node.Y - targetNode.Y);
}
private Node FindNodeWithLowestFScore(List nodeList)
{
// 找到F值最小的节点
Node lowestFScoreNode = nodeList[0];
foreach (Node node in nodeList)
{
if (node.F < lowestFScoreNode.F)
{
lowestFScoreNode = node;
}
}
return lowestFScoreNode;
}
private List GetNeighbors(Node node)
{
List neighbors = new List();
int startX = Math.Max(0, node.X - 1);
int endX = Math.Min(_width - 1, node.X + 1);
int startY = Math.Max(0, node.Y - 1);
int endY = Math.Min(_height - 1, node.Y + 1);
for (int x = startX; x <= endX; x++)
{
for (int y = startY; y <= endY; y++)
{
if (x == node.X && y == node.Y)
{

未经允许不得转载 » 本文链接:https://www.117.info/ask/fe09aAzsLBA9RBFc.html

推荐文章

  • C++多线程的应用场景有哪些

    C++多线程的应用场景包括但不限于以下几个方面: 并发处理:多线程可以用于同时执行多个任务,提高程序的并发处理能力。例如,可以用多线程处理网络请求、数据库...

  • c++ freopen函数怎么使用

    C++中的freopen函数用于将标准输入/输出重定向到指定的文件。
    使用freopen函数的一般语法如下:
    FILE *freopen(const char *filename, const char *mo...

  • c++实现多线程的方式有哪些

    C++实现多线程的方式有以下几种: 使用C++11标准中的std::thread类:通过创建std::thread对象来创建新的线程,并指定要执行的函数或函数对象。 使用C++11标准中的...

  • C#中ExecuteNonQuery()怎么使用

    在C#中,ExecuteNonQuery()方法是用于执行不返回结果的SQL语句(如INSERT、UPDATE、DELETE等)的方法。它返回一个整数,表示被影响的行数。
    可以按照以下步...

  • java中常用的方法有哪些

    Java中常用的方法有以下几种: String类的方法:常用的字符串操作方法,如substring()、indexOf()、toUpperCase()、toLowerCase()等。 Math类的方法:常用的数学...

  • JavaScript中Window setInterval()方法的作用是什么

    JavaScript中的setInterval()方法用于在一段时间间隔后重复执行指定的函数或代码。它的作用是创建一个定时器,该定时器会每隔一定的时间间隔(以毫秒为单位)就触...

  • JavaScript中onblur事件的作用是什么

    onblur事件是在元素失去焦点时触发的事件,它的作用是在用户离开输入框或者元素时执行特定的代码。可以用来验证用户输入的数据或者执行一些其他的逻辑。例如,在...

  • Android生成签名文件的方法是什么

    生成Android签名文件的方法如下: 首先,确保安装了Java Development Kit(JDK)。 打开终端或命令提示符窗口,并导航到JDK的安装目录。 在命令行中输入以下命令...