有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java为什么equals语句不能用于广度优先搜索

我正在写一个解决8块拼图的程序,但我没有得到任何输出,我正在运行调试器,我正在生成正确的后继程序,其中一个是我想要的目标状态,但当我比较它们时,它并没有说它们是,所以我的程序只是不断循环。有人知道为什么会这样吗

public static void BFSSearch(Node start, Node goal) {
    Queue<Node> q = new LinkedList<>();
    q.add(start);
    while (!q.isEmpty()) {
        Node theNode = q.poll();
        if (theNode.equals(goal)) {
            Stack<Node> path = new Stack<>();
            path.push(theNode);
            theNode = theNode.getParent();
            while (theNode.getParent() != null) {
                path.push(theNode);
                theNode = theNode.getParent();
            }
            path.push(theNode);
            int stackSize = path.size();
            for (int i = 0; i < stackSize; i++) {
                theNode = path.pop();
                theNode.printState();
            }
        } else {
            ArrayList<Node> successors = theNode.genSuccessors();
            for (int i = 0; i < successors.size(); i++) {
                Node newNode = new Node(successors.get(i).curr);
                if (newNode.equals(goal)){
                    System.out.println("found");
                }
                if (!checkRepeats(newNode)) {
                    ((LinkedList<Node>) q).add(newNode);
                }
            }
        }
    }
}

共 (0) 个答案