gpt4 book ai didi

c# - A*(A 星)算法帮助

转载 作者:太空宇宙 更新时间:2023-11-03 16:45:30 27 4
gpt4 key购买 nike

嗯,这是我更新的代码。它不会减速,但不会出现路径。

public static IntPosition[] GetPath(BlockType[,] blocks, IntPosition start, IntPosition end, int threshhold)
{
List<Node> MapNodes = new List<Node>();
MapNodes.Add(new Node() { Position = start });

bool[,] explored = new bool[blocks.GetLength(0), blocks.GetLength(1)];

explored[start.X, start.Y] = true;

int? endNode = null;

int index = 0;
while (endNode == null && index < threshhold)
{
List<Node> addNodes = new List<Node>();


foreach (Node n in MapNodes)
{
//left
if (n.Position.X - 1 >= 0)
if (explored[n.Position.X - 1, n.Position.Y] == false)
if (blocks[n.Position.X - 1, n.Position.Y] == BlockType.Open)
{
int i = addNodes.Count;
addNodes.Add(new Node() { Position = new IntPosition(n.Position.X - 1, n.Position.Y), ParentNode = n });

explored[n.Position.X - 1, n.Position.Y] = true;

if (addNodes[i].Position == end)
endNode = i;
}

//right
if (n.Position.X + 1 <= blocks.GetLength(0) - 1)
if (explored[n.Position.X + 1, n.Position.Y] == false)
if (blocks[n.Position.X + 1, n.Position.Y] == BlockType.Open)
{
int i = addNodes.Count;
addNodes.Add(new Node() { Position = new IntPosition(n.Position.X + 1, n.Position.Y), ParentNode = n });

explored[n.Position.X + 1, n.Position.Y] = true;

if (addNodes[i].Position == end)
endNode = i;
}

//up
if (n.Position.Y - 1 >= 0)
if (explored[n.Position.X, n.Position.Y - 1] == false)
if (blocks[n.Position.X, n.Position.Y - 1] == BlockType.Open)
{
int i = addNodes.Count;
addNodes.Add(new Node() { Position = new IntPosition(n.Position.X, n.Position.Y - 1), ParentNode = n });

explored[n.Position.X, n.Position.Y - 1] = true;

if (addNodes[i].Position == end)
endNode = i;
}

//down
if (n.Position.Y + 1 <= blocks.GetLength(1))
if (explored[n.Position.X, n.Position.Y + 1] == false)
if (blocks[n.Position.X, n.Position.Y + 1] == BlockType.Open)
{
int i = addNodes.Count;
addNodes.Add(new Node() { Position = new IntPosition(n.Position.X, n.Position.Y + 1), ParentNode = n });

explored[n.Position.X, n.Position.Y + 1] = true;

if (addNodes[i].Position == end)
endNode = i;
}
}

MapNodes.AddRange(addNodes);

index++;
}

if (endNode == null)
return new IntPosition[] { };
else
{
List<IntPosition> path = new List<IntPosition>();

path.Add(end);

Node CurrentNode = (MapNodes[(int)endNode].ParentNode);
bool endReached = false;

while (endReached == false)
{
path.Add(CurrentNode.Position);

if (CurrentNode.Position == start)
endReached = true;
else
CurrentNode = CurrentNode.ParentNode;
}


path.Reverse();
return path.ToArray();
}
}



public class IntPosition
{
public int X;
public int Y;

public IntPosition(int x, int y)
{
X = x;
Y = y;
}

public IntPosition() { X = 0; Y = 0; }

public override bool Equals(object obj)
{
return ((IntPosition)obj).X == X && ((IntPosition)obj).Y == Y;
}
}

最佳答案

代码有几个问题。首先,您的 X + 1 和 Y + 1 检查进行了错误的比较(大于而不是小于)。我怀疑这就是导致算法失败的原因。

其次,虽然我不熟悉该算法,但我怀疑您需要做一些事情来确保您已经检查过的节点在后续迭代中不会再次检查。此外,您需要确保不要向已经存在的 MapNodes 添加节点。这些因素的结合可能是您所看到的速度缓慢的原因,因为它会导致具有许多冗余节点的 MapNodes 呈指数增长。

关于c# - A*(A 星)算法帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6241575/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com