[英]Java - Boggle / Word Matrix Solver Path Issues
我正在制作一個應用程序,它將查找可以在4x4網格(Boggle)上使用相鄰圖塊創建的所有單詞。 我到了可以輸入字母字符串的地方,該算法將在最佳時間內找到所有可用的單詞。 但是,我不僅要知道有效的單詞,還要知道它們在板上的位置。
這是主要的游戲類。 該算法以一個字母遞歸開始,然后檢查是否存在以該字母加上其鄰居開頭的單詞。 如果不存在任何單詞,則路徑將被阻塞,並且算法將移至下一個字母。 如果存在帶有該前綴的單詞,請對該鄰居執行相同的操作。
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
,但是當我嘗試以相同方式構建正方形位置的String path
時,它會包含一堆不會彌補這個詞。
任何幫助表示贊賞,謝謝。
您必須從doNeigh()
末尾的locations
刪除最后一個元素。 否則,此路徑將無限期增長。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.