From 0202dc51af7c620dd71dd5cd13711b861c5601a0 Mon Sep 17 00:00:00 2001 From: Cal <3027929699@qq.com> Date: Tue, 9 Apr 2024 14:19:54 +0800 Subject: [PATCH] fix the bug of bfs --- Assets/Scripts/BFS/BFS.cs | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/Assets/Scripts/BFS/BFS.cs b/Assets/Scripts/BFS/BFS.cs index fcc6910..03ff9e4 100644 --- a/Assets/Scripts/BFS/BFS.cs +++ b/Assets/Scripts/BFS/BFS.cs @@ -172,6 +172,7 @@ public class BFS private List _closeList = new List(); private Queue _openList = new Queue(); private Dictionary, Node> _nodeMap = new Dictionary, Node>(); + private List ids = new List(); public BFS(Graph graph) { @@ -186,6 +187,10 @@ public class BFS public Path FindPath(Node begin, Node end) { + if (begin == null) + throw new NullReferenceException("begin"); + if (end == null) + throw new NullReferenceException("end"); _openList.Clear(); _closeList.Clear(); _nodeMap.Clear(); @@ -220,12 +225,6 @@ public class BFS { if (this._nodeMap.TryGetValue(node, out node)) { - if (node != null) - { - Debug.Log($"{node.index} is null"); - isTrue = false; - break; - } if (!this.ids.Contains(node.index)) { @@ -234,11 +233,10 @@ public class BFS path.AddNode(new PathNode(node.index, node.data)); } } - - if (node == begin) + + if (node==null || node == begin) { Debug.Log("结束了"); - isTrue = false; break; } } @@ -247,9 +245,6 @@ public class BFS return path; } -// add - private List ids = new List(); - private void Search(Node node) { var readOnlyList = this._graph.GetNeighbours(node); -- 2.25.1