/// <summary> /// 求解 /// </summary> /// <returns></returns> public Path resolve() { Path path = new Path(); Node start = new Node(p.Start); LinkedList<Node> Open = new LinkedList<Node>(); HashSet <Node>Open2=new HashSet<Node> (); // 元素与Open一样。效率不同。 HashSet<Node> Close = new HashSet<Node>(); Open.AddFirst(start); // 起始点。 Open2.Add(start); bool flag = false; Node curr=null; while(Open .Count >0) { curr = Open.First(); // 取出第一个。 Open.RemoveFirst(); Open2.Remove (curr); Close.Add(curr); // 放入闭集合,后面会做重复性检查。 if (curr.cell.End) { flag = true; break; } curr.Expand(Open, Open2, Close); //展开此节点:其所有的不在Open和Close的子节点,加入到队列尾部。 } if (flag) { while(curr.Parent !=null)//记录下所有的结果。 { path.cells.Add(curr.cell); curr = curr.Parent; } path.cells.Add(curr.cell);//这是第一个点。 } return path; }
/// <summary> /// 已经选择了某节点next后,将其在数据结构中进行安置。 /// Open 必须为双向链表。 /// Open2 是为了定位效率。 /// </summary> /// <param name="next"></param> /// <param name="Open"></param> /// <param name="Open2"></param> /// <param name="Close"></param> public void Conduct(Node next,LinkedList<Node>Open,HashSet <Node >Open2,HashSet <Node>Close) { if (!Open2.Contains(next) && !Close.Contains(next)) { Open.AddLast(next); Open2.Add(next); this.Children.Add(next); next.Parent = this; } }
public Path resolve(int pdepth) { Path path = new Path(); Node start = new Node(p.Start); start.Depth = 0; LinkedList<Node> Open = new LinkedList<Node>(); HashSet<Node> Open2 = new HashSet<Node>(); HashSet<Node> Close = new HashSet<Node>(); Open.AddFirst(start); Open2.Add(start); bool flag = false; Node curr = null; while (Open.Count > 0) { curr = Open.First(); Open.RemoveFirst(); Open2.Remove(curr); Close.Add(curr); if (curr.cell.End) { flag = true; break; } if(curr.Depth < pdepth ) curr.ExpandDF(Open, Open2, Close); } if (flag) { while (curr.Parent != null) { path.cells.Add(curr.cell); curr = curr.Parent; } path.cells.Add(curr.cell); } else ; return path; }
/// <summary> /// 是否为某节点的祖先。 /// </summary> /// <param name="offspring"></param> /// <returns></returns> public bool IsParentOf(Node offspring) { Node curr = offspring; while (curr.Parent != null) { if (this.Equals(curr.Parent)) return true; curr = curr.Parent; } return false; }
/// <summary> /// 对当前节点进行展开,使用深度优先。 /// </summary> /// <param name="Open"></param> /// <param name="Open2"></param> /// <param name="Close"></param> public void ExpandDF(LinkedList<Node> Open, HashSet<Node> Open2, HashSet<Node> Close) { Node curr = this; if (curr.cell.UpStatus == WallStatus.open && curr.cell.upNei != null) { Node next = new Node(curr.cell.upNei); curr.ConductDF(next, Open, Open2, Close); } if (curr.cell.DownStatus == WallStatus.open && curr.cell.downNei != null) { Node next = new Node(curr.cell.downNei); curr.ConductDF(next, Open, Open2, Close); } if (curr.cell.LeftStatus == WallStatus.open && curr.cell.leftNei != null) { Node next = new Node(curr.cell.leftNei); curr.ConductDF(next, Open, Open2, Close); } if (curr.cell.RightStatus == WallStatus.open && curr.cell.rightNei != null) { Node next = new Node(curr.cell.rightNei); curr.ConductDF(next, Open, Open2, Close); } }