depthfirstsearchengine.java

来自「Practical AI in Java 人工智能编程」· Java 代码 · 共 39 行

JAVA
39
字号
/** * Title:        DepthFirstSearchEngine<p> * Description:  Performs a depth first search in a maze<p> * Copyright:    Copyright (c) Mark Watson, Released under Open Source Artistic License<p> * Company:      Mark Watson Associates<p> * @author Mark Watson * @version 1.0 */import java.awt.Dimension;public class DepthFirstSearchEngine extends AbstractSearchEngine {    public DepthFirstSearchEngine(int width, int height) {        super(width, height);        iterateSearch(startLoc, 1);    }    private void iterateSearch(Dimension loc, int depth) {        if (isSearching == false) return;        maze.setValue(loc.width, loc.height, (short)depth);        Dimension [] moves = getPossibleMoves(loc);        for (int i=0; i<4; i++) {            if (moves[i] == null) break; // out of possible moves from this location            searchPath[depth] = moves[i];            if (equals(moves[i], goalLoc)) {                System.out.println("Found the goal at " + moves[i].width +                                   ", " + moves[i].height);                isSearching = false;                maxDepth = depth;                return;            } else {                iterateSearch(moves[i], depth + 1);                if (isSearching == false) return;            }        }        return;    }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?