Compare commits

...

3 Commits

Author SHA1 Message Date
zxl 2ed30ebefd Merge pull request 'master' (#2) from Cal/Frame:master into master
Reviewed-on: #2
2024-04-09 14:24:32 +08:00
Cal 0202dc51af fix the bug of bfs 2024-04-09 14:19:54 +08:00
Cal 47fcbbec8f Merge remote-tracking branch 'upstream/master' 2024-04-09 14:11:41 +08:00
1 changed files with 7 additions and 12 deletions

View File

@ -172,6 +172,7 @@ public class BFS<T>
private List<int> _closeList = new List<int>(); private List<int> _closeList = new List<int>();
private Queue<int> _openList = new Queue<int>(); private Queue<int> _openList = new Queue<int>();
private Dictionary<Node<T>, Node<T>> _nodeMap = new Dictionary<Node<T>, Node<T>>(); private Dictionary<Node<T>, Node<T>> _nodeMap = new Dictionary<Node<T>, Node<T>>();
private List<int> ids = new List<int>();
public BFS(Graph<T> graph) public BFS(Graph<T> graph)
{ {
@ -186,6 +187,10 @@ public class BFS<T>
public Path<T> FindPath(Node<T> begin, Node<T> end) public Path<T> FindPath(Node<T> begin, Node<T> end)
{ {
if (begin == null)
throw new NullReferenceException("begin");
if (end == null)
throw new NullReferenceException("end");
_openList.Clear(); _openList.Clear();
_closeList.Clear(); _closeList.Clear();
_nodeMap.Clear(); _nodeMap.Clear();
@ -220,12 +225,6 @@ public class BFS<T>
{ {
if (this._nodeMap.TryGetValue(node, out node)) 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)) if (!this.ids.Contains(node.index))
{ {
@ -234,11 +233,10 @@ public class BFS<T>
path.AddNode(new PathNode<T>(node.index, node.data)); path.AddNode(new PathNode<T>(node.index, node.data));
} }
} }
if (node == begin) if (node==null || node == begin)
{ {
Debug.Log("结束了"); Debug.Log("结束了");
isTrue = false;
break; break;
} }
} }
@ -247,9 +245,6 @@ public class BFS<T>
return path; return path;
} }
// add
private List<int> ids = new List<int>();
private void Search(Node<T> node) private void Search(Node<T> node)
{ {
var readOnlyList = this._graph.GetNeighbours(node); var readOnlyList = this._graph.GetNeighbours(node);