我已经为 Java 中的 8 个难题问题实现了深度优先搜索(递归):
protected PuzzleState depthFirstSearch(PuzzleState state) {
PuzzleState start = this.getStartState();
PuzzleState goal = this.getGoalState();
PuzzleState stop = null;
int limit = 35;
int depth = state.getDepth();
boolean tooDeep = false;
if (state.equals(goal)) {
return state;
} else {
if (depth == limit) {
return stop;
} else {
Collection<Integer> actions = PuzzleAction.getPuzzleActions();
for (Integer action : actions) {
PuzzleState starter = start;
PuzzleState next = state.succ(action);
if (next != null) {
starter = depthFirstSearch(next);
}
if (starter == stop) {
tooDeep = true;
} else {
if (!starter.equals(start)) {
return starter;
}
}
}
}
}
if (tooDeep)
return stop;
else
return start;
}
我不知道我必须改变什么才能将其转换为迭代加深深度搜索。我知道深度没有限制,因为它在每一轮中都会增加。试过这个:
protected PuzzleState iterativeDeepSearch(PuzzleState state) {
int depth = state.getDepth();
for(int limit = 1; limit < depth; limit ++){
depthFirstSearch(state, limit);
}
}
有谁知道如何将其更改为所需的IDS?先感谢您!