- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我开始创建 A* 算法。玩家应该从左下角走到右上角。每个单元有不同的字段成本。成本为 0 意味着单元不可步行。
有时玩家会跳来跳去,因为在我的封闭列表中我没有最佳路径,那里也有一些“错误”的单元格。
你可以在这里看看
https://cdn.discordapp.com/attachments/370618181892571136/446345906464358410/zgzugzzu.gif
我有一个 map
对象,它创建 map 并将所有 cell
对象保存在 cell
类型的二维数组中。每个单元格都知道自己的字段成本,但它们不需要知道自己的位置,因为数组通过传入 x 和 y 值就知道了。
一些解释说,我必须存储当前检查节点的前身,并将使用此路径时的成本当前值与其他路径进行比较。
我该怎么做?
我当前的代码
private List < Vector2Int > openCells = new List < Vector2Int > ();
private List < Vector2Int > closedCells = new List < Vector2Int > ();
private void CalculatePath() {
openCells.Clear();
closedCells.Clear();
openCells.Add(Settings.startPosition);
Vector2Int currentCellPosition = Settings.startPosition;
while (!PositionEquals(currentCellPosition, Settings.targetPosition) && openCells.Count > 0) {
Vector2Int cheapestCellPosition = openCells
.OrderBy(x => GetCostToTarget(x, Settings.targetPosition) + GetCell(x).Cost)
.First();
AddNeighbourPosition(cheapestCellPosition, Vector2Int.up);
AddNeighbourPosition(cheapestCellPosition, Vector2Int.left);
AddNeighbourPosition(cheapestCellPosition, Vector2Int.down);
AddNeighbourPosition(cheapestCellPosition, Vector2Int.right);
closedCells.Add(cheapestCellPosition);
openCells.Remove(cheapestCellPosition);
currentCellPosition = cheapestCellPosition;
}
}
private void AddNeighbourPosition(Vector2Int currentPosition, Vector2Int neighbourDirection) {
Vector2Int targetPosition = currentPosition + neighbourDirection;
if (CellExistsOnMap(targetPosition)) {
if (CellIsWalkable(targetPosition)) {
if (!CellExamined(targetPosition)) {
openCells.Add(targetPosition);
}
}
}
}
private bool PositionEquals(Vector2Int startPosition, Vector2Int targetPosition) {
return startPosition.Equals(targetPosition);
}
private bool CellIsWalkable(Vector2Int position) {
return GetCell(position).Cost != 0;
}
private Cell GetCell(Vector2Int position) {
return map.Cells[position.x, position.y];
}
private int GetCostToTarget(Vector2Int startPosition, Vector2Int targetPosition) {
return Mathf.Abs(startPosition.x - targetPosition.x) + Mathf.Abs(startPosition.y - targetPosition.y);
}
private bool CellExistsOnMap(Vector2Int position) {
int horizontalLength = Settings.fields.GetLength(0);
int verticalLength = Settings.fields.GetLength(1);
Rect mapRect = new Rect(0, 0, horizontalLength, verticalLength);
return mapRect.Contains(position);
}
private bool CellExamined(Vector2Int position) {
return openCells.Contains(position) || closedCells.Contains(position);
}
最佳答案
首先你没有正确实现 A* 并且遗漏了一些要点,
为了保持对当前单元格前身的引用,你需要改变你的单元格数据结构,建议是避免GC分配的结构,但结构不能有与结构相同类型的字段来存储前身因为它创建了一个循环。如果您的 map 尺寸很小,并且寻路过程不是按单元调用 advance ,则使用类实现它就可以了。
public class Cell
{
public Vector2Int Position { get;set;}
public int Cost {get;set;}
public Cell Parent {get;set;}
}
另一种选择是将 LinkedList 用于您的封闭列表以从目标单元格回溯到当前单元格。
你还没有分享你计算结束后如何从 closedlist 中找到路径的代码,我相信跳跃问题来自那里
关于c# - 从 A* 中获取最优路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50448297/
我正在尝试在r中编写代码,以便找到单变量正态分布的最大似然(而不是对数似然)值。我知道还有其他方法,但是我需要深入了解数值优化才能进行进一步的工作。当我调用'optim'函数时,它似乎根本不会进行迭代
最近我一直在用 php + mysql 做一个相当大的项目。现在我担心我的 mysql。我应该怎么做才能使我的 mysql 尽可能优化?把你知道的都说出来,我将非常感激。 第二个问题,我在每次加载页面
我不太了解 InitializeCriticalSectionAndSpinCount 的文档: http://msdn.microsoft.com/en-us/library/windows/des
我们公司有几种不同的获取潜在客户的方式,以及我们处理的几种类型的潜在客户。每种类型的潜在客户之间只有微小的差异,并且大部分信息与一种或多种其他潜在客户类型共享或相关。我和我的团队正在尝试使用 Solr
ϵ-贪婪策略 我知道 Q-learning 算法应该尝试在探索和利用之间取得平衡。由于我是该领域的初学者,因此我想实现一个简单版本的探索/利用行为。最佳 epsilon 值 我的实现使用 ϵ 贪婪策略
我是一名优秀的程序员,十分优秀!