8个拼图迭代深度搜索实现

时间:2016-11-21 14:15:39

标签: java search 8-puzzle iterative-deepening

我已经为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? 提前谢谢!

0 个答案:

没有答案