//******************** public NodeAStar(Int16[,] element, string act, NodeAStar prnt) { PuzzleClass = element; ActionClass = act; Next = null; Parent = prnt; }
public NodeAStar() { PuzzleClass = null; ActionClass = null; Next = null; Parent = null; }
//********************* public NodeAStar(Int16[,] element) { PuzzleClass = element; ActionClass = null; Next = null; Parent = null; }
//***************************************************** void RemoveVisited(NodeAStar Visited)//移除走過的路徑 { if (Visited.BACK == null && Visited.NEXT == null) { } else if (Visited.BACK == null && Visited.NEXT != null) { Pqueue.Front = Visited.NEXT; Visited.NEXT.BACK = null; Visited.BACK = Visited.NEXT = null; } else if (Visited.NEXT == null) { Pqueue.Rear = Visited.BACK; Visited.BACK.NEXT = null; Visited.BACK = Visited.NEXT = null; } else { Visited.BACK.NEXT = Visited.NEXT; Visited.NEXT.BACK = Visited.BACK; Visited.BACK = Visited.NEXT = null; } }
public NodeAStar Peek()//讀取 { if (front == null) { throw new Exception("Queue is Empty"); } NodeAStar result = front; return result; }
public NodeAStar Dequeue() { if (front == null) { throw new Exception("Queue is Empty");//擲出佇列是空值 } NodeAStar result = front; front = front.NEXT; return result; }
//***************************************************** public List<string> PathFinder(NodeAStar node)//把走過的路徑加到清單 { while (node.PARENT != null) { PathList.Add(node.ActionCLASS); NodeNumber.Add(node.NodeNUMBER); node = node.PARENT; j++; } return PathList; }
//***************************************************** private Int16[,] Clone(Int16[,] pz1, NodeAStar puzzle)//覆蓋陣列節點 { for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { pz1[i, j] = puzzle.PuzzleCLASS[i, j]; } } return pz1; }
//***************************************************** bool SimilarFinder(Int16[,] pzl) { NodeAStar current = Pqueue.Front; while (current != null) { if (SimilarArray(pzl,current.PuzzleCLASS)) { return false; } current = current.NEXT; } return true; }
void InsertionSort(NodeAStar Node)//插入排序節點 { NodeAStar Current = front; while (Node.Fn < Current.Fn || Current.NEXT != null) { Current = Current.NEXT; } if (Current.NEXT == null) { Current.NEXT = Node; } else { Node.NEXT = Current.NEXT; Current.NEXT = Node; } }
//***************************************************** public void addNode(NodeAStar puzzle)//滑鼠點擊的移動位置 { int hn; string id = SearchArray((Int16[,])puzzle.PuzzleCLASS); Int16 temp; switch (id) { case "00": { Int16[,] pz1 = new Int16[3, 3]; pz1 = Clone(pz1, puzzle); temp = pz1[1, 0]; pz1[1, 0] = pz1[0, 0]; pz1[0, 0] = temp; if (SimilarFinder(pz1)) { hn = Hn(pz1); Pqueue.Enqueue(pz1, "下", puzzle, hn); } RemoveVisited(puzzle); pz1 = new Int16[3, 3]; pz1 = Clone(pz1, puzzle); temp = pz1[0, 1]; pz1[0, 1] = pz1[0, 0]; pz1[0, 0] = temp; if (SimilarFinder(pz1)) { hn = Hn(pz1); Pqueue.Enqueue(pz1, "右", puzzle, hn); } break; } case "01": { Int16[,] pz1 = new Int16[3, 3]; pz1 = Clone(pz1, puzzle); temp = pz1[1, 1]; pz1[1, 1] = pz1[0, 1]; pz1[0, 1] = temp; if (SimilarFinder(pz1)) { hn = Hn(pz1); Pqueue.Enqueue(pz1, "下", puzzle, hn); } RemoveVisited(puzzle); pz1 = new Int16[3, 3]; pz1 = Clone(pz1, puzzle); temp = pz1[0, 0]; pz1[0, 0] = pz1[0, 1]; pz1[0, 1] = temp; if (SimilarFinder(pz1)) { hn = Hn(pz1); Pqueue.Enqueue(pz1, "左", puzzle, hn); } pz1 = new Int16[3, 3]; pz1 = Clone(pz1, puzzle); temp = pz1[0, 2]; pz1[0, 2] = pz1[0, 1]; pz1[0, 1] = temp; if (SimilarFinder(pz1)) { hn = Hn(pz1); Pqueue.Enqueue(pz1, "右", puzzle, hn); } break; } case "02": { Int16[,] pz1 = new Int16[3, 3]; pz1 = Clone(pz1, puzzle); temp = pz1[1, 2]; pz1[1, 2] = pz1[0, 2]; pz1[0, 2] = temp; if (SimilarFinder(pz1)) { hn = Hn(pz1); Pqueue.Enqueue(pz1, "下", puzzle, hn); } RemoveVisited(puzzle); pz1 = new Int16[3, 3]; pz1 = Clone(pz1, puzzle); temp = pz1[0, 1]; pz1[0, 1] = pz1[0, 2]; pz1[0, 2] = temp; if (SimilarFinder(pz1)) { hn = Hn(pz1); Pqueue.Enqueue(pz1, "左", puzzle, hn); } break; } case "10": { Int16[,] pz1 = new Int16[3, 3]; pz1 = Clone(pz1, puzzle); temp = pz1[2, 0]; pz1[2, 0] = pz1[1, 0]; pz1[1, 0] = temp; if (SimilarFinder(pz1)) { hn = Hn(pz1); Pqueue.Enqueue(pz1, "下", puzzle, hn); } RemoveVisited(puzzle); pz1 = new Int16[3, 3]; pz1 = Clone(pz1, puzzle); temp = pz1[0, 0]; pz1[0, 0] = pz1[1, 0]; pz1[1, 0] = temp; if (SimilarFinder(pz1)) { hn = Hn(pz1); Pqueue.Enqueue(pz1, "下", puzzle, hn); } pz1 = new Int16[3, 3]; pz1 = Clone(pz1, puzzle); temp = pz1[1, 1]; pz1[1, 1] = pz1[1, 0]; pz1[1, 0] = temp; if (SimilarFinder(pz1)) { hn = Hn(pz1); Pqueue.Enqueue(pz1, "右", puzzle, hn); } break; } case "11": { Int16[,] pz1 = new Int16[3, 3]; pz1 = Clone(pz1, puzzle); temp = pz1[2, 1]; pz1[2, 1] = pz1[1, 1]; pz1[1, 1] = temp; if (SimilarFinder(pz1)) { hn = Hn(pz1); Pqueue.Enqueue(pz1, "下", puzzle, hn); } RemoveVisited(puzzle); pz1 = new Int16[3, 3]; pz1 = Clone(pz1, puzzle); temp = pz1[0, 1]; pz1[0, 1] = pz1[1, 1]; pz1[1, 1] = temp; if (SimilarFinder(pz1)) { hn = Hn(pz1); Pqueue.Enqueue(pz1, "上", puzzle, hn); } pz1 = new Int16[3, 3]; pz1 = Clone(pz1, puzzle); temp = pz1[1, 0]; pz1[1, 0] = pz1[1, 1]; pz1[1, 1] = temp; if (SimilarFinder(pz1)) { hn = Hn(pz1); Pqueue.Enqueue(pz1, "左", puzzle, hn); } pz1 = new Int16[3, 3]; pz1 = Clone(pz1, puzzle); temp = pz1[1, 2]; pz1[1, 2] = pz1[1, 1]; pz1[1, 1] = temp; if (SimilarFinder(pz1)) { hn = Hn(pz1); Pqueue.Enqueue(pz1, "右", puzzle, hn); } break; } case "12": { Int16[,] pz1 = new Int16[3, 3]; pz1 = Clone(pz1, puzzle); temp = pz1[2, 2]; pz1[2, 2] = pz1[1, 2]; pz1[1, 2] = temp; if (SimilarFinder(pz1)) { hn = Hn(pz1); Pqueue.Enqueue(pz1, "下", puzzle, hn); } RemoveVisited(puzzle); pz1 = new Int16[3, 3]; pz1 = Clone(pz1, puzzle); temp = pz1[0, 2]; pz1[0, 2] = pz1[1, 2]; pz1[1, 2] = temp; if (SimilarFinder(pz1)) { hn = Hn(pz1); Pqueue.Enqueue(pz1, "上", puzzle, hn); } pz1 = new Int16[3, 3]; pz1 = Clone(pz1, puzzle); temp = pz1[1, 1]; pz1[1, 1] = pz1[1, 2]; pz1[1, 2] = temp; if (SimilarFinder(pz1)) { hn = Hn(pz1); Pqueue.Enqueue(pz1, "左", puzzle, hn); } break; } case "20": { Int16[,] pz1 = new Int16[3, 3]; pz1 = Clone(pz1, puzzle); temp = pz1[1, 0]; pz1[1, 0] = pz1[2, 0]; pz1[2, 0] = temp; if (SimilarFinder(pz1)) { hn = Hn(pz1); Pqueue.Enqueue(pz1, "上", puzzle, hn); } RemoveVisited(puzzle); pz1 = new Int16[3, 3]; pz1 = Clone(pz1, puzzle); temp = pz1[2, 1]; pz1[2, 1] = pz1[2, 0]; pz1[2, 0] = temp; if (SimilarFinder(pz1)) { hn = Hn(pz1); Pqueue.Enqueue(pz1, "右", puzzle, hn); } break; } case "21": { Int16[,] pz1 = new Int16[3, 3]; pz1 = Clone(pz1, puzzle); temp = pz1[1, 1]; pz1[1, 1] = pz1[2, 1]; pz1[2, 1] = temp; if (SimilarFinder(pz1)) { hn = Hn(pz1); Pqueue.Enqueue(pz1, "上", puzzle, hn); } RemoveVisited(puzzle); pz1 = new Int16[3, 3]; pz1 = Clone(pz1, puzzle); temp = pz1[2, 0]; pz1[2, 0] = pz1[2, 1]; pz1[2, 1] = temp; if (SimilarFinder(pz1)) { hn = Hn(pz1); Pqueue.Enqueue(pz1, "左", puzzle, hn); } pz1 = new Int16[3, 3]; pz1 = Clone(pz1, puzzle); temp = pz1[2, 2]; pz1[2, 2] = pz1[2, 1]; pz1[2, 1] = temp; if (SimilarFinder(pz1)) { hn = Hn(pz1); Pqueue.Enqueue(pz1, "右", puzzle, hn); } break; } case "22": { Int16[,] pz1 = new Int16[3, 3]; pz1 = Clone(pz1, puzzle); temp = pz1[1, 2]; pz1[1, 2] = pz1[2, 2]; pz1[2, 2] = temp; if (SimilarFinder(pz1)) { hn = Hn(pz1); Pqueue.Enqueue(pz1, "上", puzzle, hn); } RemoveVisited(puzzle); pz1 = new Int16[3, 3]; pz1 = Clone(pz1, puzzle); temp = pz1[2, 1]; pz1[2, 1] = pz1[2, 2]; pz1[2, 2] = temp; if (SimilarFinder(pz1)) { hn = Hn(pz1); Pqueue.Enqueue(pz1, "左", puzzle, hn); } break; } } }
public void Clear()//清除 { front = rear = null; }
public PQueue() { }//先進先出法的佇列 public void Enqueue(Int16[,] pzl, string act, NodeAStar parent, int hn)//加入佇列 { NodeAStar newNode = new NodeAStar(pzl, act, parent); if (parent != null)//如果存節點節點的空間不等於空值 { newNode.DEPTH = parent.DEPTH + 1;//步數+1 newNode.Fn = newNode.DEPTH + hn; } if (rear == null)//如果尾等於空值 { newNode.DEPTH = 0; newNode.Fn = hn; front = newNode; newNode.NodeNUMBER = count; rear = front;//尾等於頭 } else { NodeAStar Current = front;//當前的值在第一個 while (newNode.Fn > Current.Fn && Current.NEXT != null) { Current = Current.NEXT; } if (Current.BACK == null && Current.NEXT == null) { newNode.DEPTH = parent.DEPTH + 1; newNode.Fn = newNode.DEPTH + hn; front = newNode; front.NEXT = Current; Current.BACK = front; rear = front.NEXT; } else if (Current.BACK == null && Current.NEXT != null) { newNode.DEPTH = parent.DEPTH + 1; front = newNode; front.NEXT = Current; Current.BACK = front; } else if (Current.NEXT == null && newNode.Fn > Current.Fn) { newNode.DEPTH = parent.DEPTH + 1; Current.NEXT = newNode; newNode.BACK = Current; rear = newNode; } else { newNode.DEPTH = parent.DEPTH + 1; newNode.NEXT = Current; Current.BACK.NEXT = newNode; newNode.BACK = Current.BACK; Current.BACK = newNode; } newNode.NodeNUMBER = count; } count++; }