不确定如何在我的代码中实现深度优先搜索算法。以下是解决8 puzzle的广度优先搜索算法的示例:
public direction[] Solve(nPuzzle puzzle) {
//This method uses the fringe as a queue.
//Therefore, nodes are searched in order of cost, with the lowest cost
// unexplored node searched next.
//-----------------------------------------
//put the start state in the Fringe to get explored.
addToFrontier(puzzle.StartState);
ArrayList<PuzzleState> newStates = new ArrayList<PuzzleState>();
while(Frontier.size() > 0)
{
//get the next item off the fringe
PuzzleState thisState = popFrontier();
//is it the goal item?
if(thisState.equals(puzzle.GoalState))
{
//We have found a solution! return it!
return thisState.GetPathToState();
}
else
{
//This isn't the goal, just explore the node
newStates = thisState.explore();
for(int i = 0; i < newStates.size(); i++)
{
//add this state to the fringe, addToFringe() will take care of duplicates
addToFrontier(newStates.get(i));
}
}
}
//No solution found and we've run out of nodes to search
//return null.
return null;
}
还有更多代码here 非常感谢任何帮助
答案 0 :(得分:0)
一个。实现的主要区别在于:在DFS中,要探索的节点存储在stck中,而在BFS中,它们存储在队列中。
湾对于您试图解决的难题,我认为BFS更合适,因为您正在搜索最短路径。
要获得mcve后的更多帮助,我们就不必进行大量的猜测。