我一直在尝试在 Java 中实现迭代深化搜索。但是,由于某种原因,并不是每个节点的所有子节点都被访问,导致结果不正确。到目前为止,这是我的代码:
public int IDS(Node start, Node goal){
int depth = 0; //set starting depth to 0
Node current=start; //current node is equal to start
int goalNode=0; //goalNode is originally set to 0
//List<Node> tempList=new ArrayList<Node>();
while(goalNode==0){ //while goalNode is equal to 0
List<Node> visited=new ArrayList<Node>(); //create an array list of nodes
goalNode=DLS(current, goal, depth, visited);
depth++; //increment the depth
}
System.out.println("RESULT");
return goalNode;
}
public int DLS(Node current, Node goal, int depth, List<Node> visited){
if(depth>=0){
if ( current == goal ){ //stop program
System.out.println("REACHED GOAL");
return current.value;
}else{
visited.add(current); //add the current node to visited list (in the beginning =start)
List<Node> temp = Adjacency_List.get(current.value); //get children of current node
for(Node node: temp){ //for each child
System.out.println("Current Node: "+current.value);
System.out.println(current.value + " - " + node.value);
if(node != null && !visited.contains(node)){
//tempList.add(node);
return DLS(node, goal, depth-1, visited);
}
}
}
}else{
return 0;
}
return 0;
}