簡體   English   中英

調試遞歸方法,用於查找boggle board中是否存在單詞

[英]Debug recursive method for finding if a word exists in a boggle board

我正在嘗試實現一個boggle求解器。

我的基本想法是創建一個方法來檢查一個單詞,看它是否在板上。 然后通過刪除以char不在板上的單詞來修剪我的字典,然后將該方法應用於字典集上的每個單詞以獲得解決方案集。

我不完全確定這個解決方案的效率如何......我很確定它只是在O(n)上運行(與字典集的大小成比例) - 這對於更大的板(5x5 - 7x7)會更好

我當前的方法(如果我可以修復訪問的方式,它應該工作):

private Tile findFirstTile(String word) {
    word = word.toUpperCase();
    Tile first = null;
    boolean found = false;
    for (Tile tile : tiles) {
        if (tile.getChar() == word.charAt(0)) {
            first = tile;
            found = true;
        }
    }
    if (found) {
        System.out.println("Found the tile!!");
        return first;
    }
    else return null;
}


public boolean findWordOnBoard(String word, Tile tile, int depth, HashSet<Integer> visited) {
    System.out.println("depth is " + String.valueOf(depth) + " right meow.");
    if (depth == word.length()) return true; // base case - breaks recursion (on board)
    else {
        word = word.toUpperCase();
        if (tile == null) return false;
        HashSet<Integer> neighbors = map.get(tile.getPlace());
        for (int n : neighbors) {
            if ((tiles[n-1].getChar() == word.charAt(depth)) && (!visited.contains(n))) {
                visited.add(n);
                System.out.println("found " + tile.getChar() + " at " + n);
                if (depth == word.length()) return true; // it shouldn't but oh well it's just here
                findWordOnBoard(word, tiles[n-1], depth +1, visited);
            } 
        }

        System.out.println("only supposed to be here if it's ACTUALLY not on board");
        return false; //will only get here if it doesn't find a new word
    }
}

我不確定我是否正確實現了遞歸..它現在沒有找到任何單詞,但在我看來它應該有效..? 我特別擔心的是我是否正確處理了訪問集(如果它正在跟蹤每個深度訪問的哪些圖塊),但我知道這不是唯一的問題,因為我仍然可以找到一些簡短的單詞。 ..

R L O S
E E A P
M S T R
E A T S

另外,我剛剛意識到我的“findFirstTile”方法只會開始在最后一個以該字母開頭的圖塊上查找單詞...所以如果在板上多次出現該字母,它可能無法查看所有字母。

這是我的Tile對象的構造函數:

  public Tile (char letter, int place){ // NOTE: the char MUST BE capital
    this.letter = letter;
    this.place = place;
    try {
        img = ImageIO.read(new File("tile"+letter+".png"));
    } catch (IOException e) {
    }

我引用的Tile數組(tile)也只是所有tile的數組,所以基本上在我的板上:

tiles[0]  tiles[1]  tiles[2]  tiles[3]
tiles[4]  tiles[5]  tiles[6]  tiles[7]
tiles[8]  tiles[9]  tiles[10] tiles[11]
tiles[12] tiles[13] tiles[14] tiles[15]

而“地方”(來自Tile構造函數)只是

1  2  3  4
5  6  7  8
9  10 11 12
13 14 15 16

我檢查了我的getNeighbors()和getChar()以及getPlace()方法,它們都按預期工作。

如果有人想知道(可能不是,哈哈)

這是修改后的(和工作)代碼:

public boolean findWordOnBoard(String word, Tile tile, int depth, HashSet<Integer> visited) {
    if (depth == word.length()) return true; // base case - breaks recursion (on board)
    else {
        word = word.toUpperCase();
        if (tile == null) return false;
        HashSet<Integer> neighbors = map.get(tile.getPlace());
        for (int n : neighbors) {
            if (depth >= word.length()) return true;
            if ((tiles[n-1].getChar() == word.charAt(depth)) && (!visited.contains(n))) {
                visited.add(n);
                System.out.println("found " + tile.getChar() + " at " + n);
                return findWordOnBoard(word, tiles[n-1], depth+1, visited);
            } 
        }

    }
    return false; //will only get here if it doesn't find a new word
}

問題只是返回遞歸調用,而不是只是使它,因為當它向后通過遞歸時,基礎案例調用break(depth == word.length())在深度恢復為1時停止為真。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM