我一直在嘗試在Java中實現迭代深化搜索。但是,出於某種原因,並非所有的孩子,對於每個節點都正在訪問,導致不正確的結果。這是我到目前爲止的代碼:迭代深化搜索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;
}
請提供MCVE:https://stackoverflow.com/help/mcve –
@UeliHofstetter這是一個有點很難在這個問題中包含更多的代碼,因爲這是針對學校作業的,我不能再在線上放置代碼。然而,用於迭代深化搜索算法的工作僞代碼會很好(因爲最初只有開始節點和目標節點是已知的)。 – Questionnaire