Objects 列表在应用 Dijkstra 算法实现时被删除
Objects in list get removed when applying Dijkstra's Algorithm implementation
我不知道如何更好地制定问题标题而又不会过于描述性,提前抱歉...
总之,我的问题如下。
我有一个 List NodeList 和一个名为 Unvisited 的辅助 List。
我在未访问列表中使用方法 GetPath(它是 Dijkstra 的 Pathfidning 算法的一个实现)。但是由于一些奇怪的原因,当我绘制存储在 NodeList 中的节点中的纹理时,一些节点(特别是用于追踪路径的节点)被删除了。
我正在寻找一个解释,为什么即使我明确设置 Unvisited 等于 NodeList,节点也会从 NodeList 中删除...
编辑:如果缺少任何理解问题的代码,请询问,我会编辑!
相关代码:
public class Hotel
{
public List<Node> nodeList;
//constructor loadscontent and initialises list, ommitted here.
public void BuildHotel(ContentManager content)
{
for (int i = 0; i < 5; i++)
{
GuestRoom temp = new GuestRoom(100 + i, content, new Point(64 + (i * 64), 128), new Point(2, 1));
nodeList.Add(new Node(temp, new Point(64 + (i * 64), 128)));
}
// add edges between some nodes
for (int i = 0; i < 4; i++)
{
AddEdge(nodeList[i].Room.RoomId, nodeList[i + 1].Room.RoomId, 2);
}
guest = new Guest(content);
guest.Setpath(100, 104, nodeList);
}
}
class PathFinding
{
public List<Node> Unvisited;
public List<Node> Visited;
private Stack<Node> _temp = new Stack<Node>();
public Stack<Node> GetPath(int startroom, int finalroom, List<Node> nodeList)
{
Unvisited = nodeList;
Node startNode = Unvisited.DefaultIfEmpty(null).FirstOrDefault(x => x.Room.RoomId == startroom);
Node finalNode = Unvisited.DefaultIfEmpty(null).FirstOrDefault(x => x.Room.RoomId == finalroom);
if (startNode == null || finalNode == null)
{
Console.WriteLine("At least one of the nodes does not exist");
return null;
}
startNode.Distance = 0;
Node currentNode = startNode;
while (!IsVisited(currentNode, finalNode))
{
currentNode = Unvisited.Aggregate((l, r) => l.Distance < r.Distance ? l : r);
}
//reverse nodes in queue
Queue<Node> reversedqueue = MakePath(startNode, currentNode, finalNode);
for (int i = 0; i < MakePath(startNode, currentNode, finalNode).Count; i++)
{
_temp.Push(reversedqueue.Dequeue());
}
return _temp;
}
}
public class SimulationScreen : Screen
{
private Hotel hotel;
//.. other methods ommited.
public override void Activate(bool instancePreserved)
{
if (!instancePreserved)
{
if (_content == null)
_content = new ContentManager(ScreenManager.Game.Services, "Content");
ScreenManager.Game.ResetElapsedTime();
}
hotel = new Hotel(_content);
}
}
错误的视觉表示
没有打开探路者
您的问题就在这里:
Unvisited = nodeList;
That's the problem, I have looked through my entire solution and there is not a single place where the nodes get removed from the NodeList ._. any removal from the list is from the Unvisited list... :s
在该分配之后,从 Unvisited 列表中删除的任何内容都会从 nodeList 中删除。 List 是一种引用类型,因此当您通过赋值更改它的值时,您实际上是在更改它所引用的对象。在此之后,Unvisited 和 nodeList 都引用同一个对象。为避免这种情况,请使用旧列表实例化一个新列表,而不是将两个列表分配给同一引用:
Unvisited = new List<Node>(nodeList);
我不知道如何更好地制定问题标题而又不会过于描述性,提前抱歉...
总之,我的问题如下。
我有一个 List NodeList 和一个名为 Unvisited 的辅助 List。
我在未访问列表中使用方法 GetPath(它是 Dijkstra 的 Pathfidning 算法的一个实现)。但是由于一些奇怪的原因,当我绘制存储在 NodeList 中的节点中的纹理时,一些节点(特别是用于追踪路径的节点)被删除了。
我正在寻找一个解释,为什么即使我明确设置 Unvisited 等于 NodeList,节点也会从 NodeList 中删除...
编辑:如果缺少任何理解问题的代码,请询问,我会编辑!
相关代码:
public class Hotel
{
public List<Node> nodeList;
//constructor loadscontent and initialises list, ommitted here.
public void BuildHotel(ContentManager content)
{
for (int i = 0; i < 5; i++)
{
GuestRoom temp = new GuestRoom(100 + i, content, new Point(64 + (i * 64), 128), new Point(2, 1));
nodeList.Add(new Node(temp, new Point(64 + (i * 64), 128)));
}
// add edges between some nodes
for (int i = 0; i < 4; i++)
{
AddEdge(nodeList[i].Room.RoomId, nodeList[i + 1].Room.RoomId, 2);
}
guest = new Guest(content);
guest.Setpath(100, 104, nodeList);
}
}
class PathFinding
{
public List<Node> Unvisited;
public List<Node> Visited;
private Stack<Node> _temp = new Stack<Node>();
public Stack<Node> GetPath(int startroom, int finalroom, List<Node> nodeList)
{
Unvisited = nodeList;
Node startNode = Unvisited.DefaultIfEmpty(null).FirstOrDefault(x => x.Room.RoomId == startroom);
Node finalNode = Unvisited.DefaultIfEmpty(null).FirstOrDefault(x => x.Room.RoomId == finalroom);
if (startNode == null || finalNode == null)
{
Console.WriteLine("At least one of the nodes does not exist");
return null;
}
startNode.Distance = 0;
Node currentNode = startNode;
while (!IsVisited(currentNode, finalNode))
{
currentNode = Unvisited.Aggregate((l, r) => l.Distance < r.Distance ? l : r);
}
//reverse nodes in queue
Queue<Node> reversedqueue = MakePath(startNode, currentNode, finalNode);
for (int i = 0; i < MakePath(startNode, currentNode, finalNode).Count; i++)
{
_temp.Push(reversedqueue.Dequeue());
}
return _temp;
}
}
public class SimulationScreen : Screen
{
private Hotel hotel;
//.. other methods ommited.
public override void Activate(bool instancePreserved)
{
if (!instancePreserved)
{
if (_content == null)
_content = new ContentManager(ScreenManager.Game.Services, "Content");
ScreenManager.Game.ResetElapsedTime();
}
hotel = new Hotel(_content);
}
}
您的问题就在这里:
Unvisited = nodeList;
That's the problem, I have looked through my entire solution and there is not a single place where the nodes get removed from the NodeList ._. any removal from the list is from the Unvisited list... :s
在该分配之后,从 Unvisited 列表中删除的任何内容都会从 nodeList 中删除。 List 是一种引用类型,因此当您通过赋值更改它的值时,您实际上是在更改它所引用的对象。在此之后,Unvisited 和 nodeList 都引用同一个对象。为避免这种情况,请使用旧列表实例化一个新列表,而不是将两个列表分配给同一引用:
Unvisited = new List<Node>(nodeList);