【发布时间】:2018-01-28 23:18:29
【问题描述】:
我试图避免无限循环,但无法找出问题所在。这应该为 3x2 拼图板找到解决方案。我怀疑问题可能出在我被覆盖的 equals 方法上,但我不确定。遇到两个问题:
1) 它不断重新探索已经探索过的节点。
2) 未找到解决方案前队列为空,导致错误。
驱动类:
import java.util.*;
public class Driver {
public static void main(String[] args){
Node test = new Node(new int[]{1, 4, 2, 5, 3, 0}, null);
BFS(test);
System.out.println("done");
}
public static void BFS(Node initial){
Queue<Node> queue = new LinkedList<>();
ArrayList<Node> explored = new ArrayList<>();
queue.add(initial);
Node current = initial;
while (!current.isGoal()){
current = queue.remove();
for (Node child: current.getChildren()){
if (!explored.contains(child)) queue.add(child);
}
explored.add(current);
current.print();
}
System.out.println("DONEDONEDONE");
current.printTrace();
}
public static void DFS(Node initial){
}
}
节点类:
import java.lang.reflect.Array;
import java.util.*;
public class Node {
int[] state;
Node parent;
public Node(int[] initialState, Node parent){
this.parent = parent;
this.state = initialState;
}
public boolean isGoal(){
int[] goal = {0,1,2,3,4,5};
return Arrays.equals(this.state, goal);
}
public ArrayList<Node> getChildren(){
ArrayList<Node> children = new ArrayList<>();
Integer[] newInt = new Integer[getState().length];
for (int i = 0; i < getState().length; i++) {
newInt[i] = Integer.valueOf(getState()[i]);
}
int position = Arrays.asList(newInt).indexOf(0);
switch(position){
case 0:
children.add(new Node(switchPos(0,3), this));
children.add(new Node(switchPos(0,1), this));
break;
case 1:
children.add(new Node(switchPos(1,0), this));
children.add(new Node(switchPos(1,4), this));
children.add(new Node(switchPos(1,2), this));
break;
case 2:
children.add(new Node(switchPos(2,1), this));
children.add(new Node(switchPos(2,5), this));
break;
case 3:
children.add(new Node(switchPos(3,0), this));
children.add(new Node(switchPos(3,4), this));
break;
case 4:
children.add(new Node(switchPos(4,3), this));
children.add(new Node(switchPos(4,5), this));
children.add(new Node(switchPos(4,1), this));
break;
case 5:
children.add(new Node(switchPos(5,2), this));
children.add(new Node(switchPos(5,4), this));
break;
}
return children;
}
public int[] getState(){
return this.state;
}
public int[] switchPos(int index1, int index2){
int[] newer = getState().clone();
int temp = newer[index1];
newer[index1] = newer[index2];
newer[index2] = temp;
return newer;
}
public void print(){
System.out.println("---------");
System.out.println(Arrays.toString(Arrays.copyOfRange(getState(), 0, 3)));
System.out.println(Arrays.toString(Arrays.copyOfRange(getState(), 3, 6)));
System.out.println("---------");
}
public void printTrace(){
Stack<Node> stack = new Stack<>();
Node current = this;
while (current.parent != null){
stack.push(current);
current = current.parent;
}
while (!stack.isEmpty()){
stack.pop().print();
}
}
@Override
public boolean equals(Object object){
Node node2 = (Node) object;
return (Arrays.equals(node2.getState(), this.getState()));
}
}
【问题讨论】:
标签: java algorithm artificial-intelligence equals breadth-first-search