Java - Boggle / Word Matrix 求解器路径问题

Java - Boggle / Word Matrix Solver Path Issues

我正在制作一个应用程序,它将找到所有可以用 4x4 网格 (Boggle) 上的相邻图块组成的单词。我已经到了可以输入一串字母的地方,算法将在最佳时间内找到所有可用的单词。但是,我不仅需要知道有效的单词,还需要知道它们在棋盘上的方块位置。

这是主游戏 class。该算法递归地从一个字母开始,然后检查是否存在以该字母及其相邻字母开头的单词。如果不存在任何单词,路径就会被阻断,算法会继续处理下一个字母。如果存在带有该前缀的单词,则对该邻居执行相同的操作。

import java.io.IOException;
import java.util.ArrayList;

public class Game {
private final int boardSize = 4;
private BoardSquare[][] squares;
private ArrayList<String> validWords;
private static WordTree trie;
private static int counter = 0;
private DevRunner runner;

public Game(String letters) throws IOException{
    validWords = new ArrayList<String>();
    runner = new DevRunner();
    trie = new WordTree();
    squares = new BoardSquare[boardSize][boardSize];
    for (int y=0; y<boardSize; y++){
        for (int x=0; x<boardSize; x++){
            squares[x][y] = new BoardSquare(letters.charAt(y*boardSize + x), y*boardSize + x);
        }
    }
    for (int y=0; y<boardSize; y++){
        for (int x=0; x<boardSize; x++){
            for (int a=-1; a<2; a++){
                for (int b=-1; b<2; b++){
                    if (a == 0 && b == 0) continue;
                    if (x+b < 0 || y+a < 0) continue;
                    if (x+b > boardSize-1 || y+a > boardSize-1) continue;
                    squares[x][y].addNeighbor(squares[x+b][y+a]);
                }
            }
        }
    }
    getPossibleCombinations();
    System.out.println(counter + " words found.");
}

public void getPossibleCombinations(){
    for (int y=0; y<boardSize; y++){
        for (int x=0; x<boardSize; x++){
            doNeigh(squares[x][y], "", new ArrayList<Integer>());
        }
    }
}

public void doNeigh(BoardSquare square, String path, ArrayList<Integer> locations) {
    square.setIsActive(true);
    path += square.getData();
    locations.add(square.getPosition());
    if (trie.has(path) != 0){
        for (BoardSquare neighbor : square.getNeighbors()){
            if (!neighbor.getIsActive()){
                doNeigh(neighbor, path, locations);
                };
            }
        }
    if (trie.has(path) == 1 && !validWords.contains(path)){
        System.out.print(path + " is a word! (");
        validWords.add(path);
        for (int i : locations){
            System.out.print(i + " -> ");
        }
        System.out.print("\n");
        sendWord(path);
        counter++;
    }
    square.setIsActive(false);
}

public void sendWord(String s){

}

public static void main(String[] args){
    try {
        long t1 = System.currentTimeMillis();
        Game g = new Game("SIOZTRTEBAINERLA");
        long t2 = System.currentTimeMillis();
        System.out.println("The algorithm took " + Long.toString(t2-t1) + " milliseconds to complete.");
    } catch (IOException e) {
        e.printStackTrace();
    }
}
}

实际输出:

SITAR is a word! (0 -> 1 -> 2 -> 4 -> 5 -> 8 -> 9 -> 5 -> 2 -> 6 -> 8 -> 10 -> 6 -> 7 -> 11 -> 13 -> 14 -> 15 -> 
SIT is a word! (0 -> 1 -> 2 -> 4 -> 5 -> 8 -> 9 -> 5 -> 2 -> 6 -> 8 -> 10 -> 6 -> 7 -> 11 -> 13 -> 14 -> 15 -> 6 -> 8 -> 10 -> 12 -> 13 -> 8 -> 10 -> 5 -> 6 -> 7 -> 11 -> 14 -> 15 -> 12 -> 14 -> 14 -> 
SIR is a word! (0 -> 1 -> 2 -> 4 -> 5 -> 8 -> 9 -> 5 -> 2 -> 6 -> 8 -> 10 -> 6 -> 7 -> 11 -> 13 -> 14 -> 15 -> 6 -> 8 -> 10 -> 12 -> 13 -> 8 -> 10 -> 5 -> 6 -> 7 -> 11 -> 14 -> 15 -> 12 -> 14 -> 14 -> 5 -> 2 -> 3 -> 6 -> 7 -> 4 -> 6 -> 8 -> 9 -> 10 -> 6 -> 7 -> 9 -> 11 -> 6 -> 7 -> 14 -> 15 -> 13 -> 14 -> 15 -> 

预期输出:

SITAR is a word! (0 -> 1 -> 4 -> 9 -> 5 ->
SIT is a word! (0 -> 1 -> 4 ->
SIR is a word! (0 -> 1 -> 5 ->
...

我不明白为什么我可以使用 doNeigh() 方法并让它通过递归构建 String path,但是当我尝试以相同的方式构建正方形位置的数组列表时,它包括一堆不组成单词的方块

感谢任何帮助,谢谢。

您必须删除 locationsdoNeigh() 末尾的最后一个元素。否则这条路会无限长。