簡體   English   中英

如何在Boggle游戲板上進行單詞的遞歸搜索?

[英]How to do a recursive search for a word in the Boggle game board?

有人可以通過偽代碼或什至是遞歸公式來幫助我,該遞歸公式描述了在Boggle板上對單詞的遞歸搜索,以便我可以開始使用嗎?

假設您有一個單詞列表,該單詞列表可能存儲在Trie數據結構中(我創建了一個工作的Trie,並在此處提供了有關提高其效率的注釋)。

一旦有了Trie結構(前綴樹),該結構允許您基於單詞的前綴搜索單詞,則需要使用遞歸方法,例如以下偽代碼。

char[][] gameBoard = new char[4][4];
List<String> wordList = new ArrayList<String>();
//fill in the game board with characters
//Start the word search at each letter
for(int x = 0; x < 4; x++){
    for(int y = 0; y < 4; y++){
        recursiveWordSearch(x, y, "");
    }
}
recursiveWordSearch(int x, int y, String word){
    //Concatenate gameBoard[x][y] to word.
    //Check to see if word is a valid word (check against your word list).
    //If word, add to wordList

    /*Check word list to see if any words contain current prefix. If not,
     then there's no point in continuing further (return). IE if AQZ isn't the 
     start of any word at all in the list, no reason to keep adding letters, it's
     never going to make a word.  */

    //Otherwise recursively call this method moving left/right/up/down
    recursiveWordSearch(x+1, y, word); //move right
    recursiveWordSearch(x, y+1, word); //move up
    recursiveWordSearch(x-1, y, word); //move left
    recursiveWordSearch(x, y-1, word); //move down
    /*You'll want to make sure that x-1, x+1, y-1 and y+1 are valid values before
     sending them. */


}

為了存儲有效單詞,使用檢查方法的數據結構被賦予一些有效單詞的字符串前綴,並且被賦予字符串需要一個有效單詞,例如Trie數據結構。

為了找到所有可能的有效單詞,我們必須為每個位置開始單詞,然后遞歸訪問每個未訪問的鄰居。 這是python類的兩種方法,可實現對給定表中所有有效單詞的搜索:

def solve_with( self, ind, inds_passed, word):
    word += self.table[ind[0]][ind[1]]  # Add next character
    if self.trie.is_prefix(word):       # Is current string prefix of valid word
        if len(word) > 2 and self.trie.is_word(word):  # Is current string whole word
            self.ret.add(word)
        inds_passed.add(ind)            # Set this position as visited
        for n in self.neigbours(ind):   # Pass through all neighbours
            if n not in inds_passed:    # If not visited already
                self.solve_with(n, inds_passed, word)  # Recursive call
        inds_passed.discard(ind)        # Remove position as visited

def solve(self):
    self.ret = set()                    # Set of all word found on table
    for x in xrange(0, self.dim):       # Start search with each position
        for y in xrange(0, self.dim):
            self.solve_with( (x,y), set(), '')
    return self.ret

使用DFS方法的Java實現

import java.util.Arrays;

public class WordBoggle {

static int[] dirx = { -1, 0, 0, 1 };
static int[] diry = { 0, -1, 1, 0 };

public static void main(String[] args) {
    char[][] board = { { 'A', 'B', 'C', 'E' }, { 'S', 'F', 'C', 'S' }, { 'A', 'D', 'E', 'E' } };

    String word = "ABFSADEESCCEA";
    System.out.println(exist(board, word));
}

static boolean exist(char[][] board, String word) {
    if (board == null || board.length == 0 || word == null || word.isEmpty())
        return false;
    boolean[][] visited = new boolean[board.length][board[0].length];
    for (int i = 0; i < board.length; i++) {
        resetVisited(visited);
        for (int j = 0; j < board[0].length; j++) {
            if (board[i][j] == word.charAt(i)) {
                return DFS(board, word, i, j, 1, visited);
            }
        }
    }
    return false;
}

static void resetVisited(boolean[][] visited) {
    for (int l = 0; l < visited.length; l++) {
        Arrays.fill(visited[l], false);
    }
}

static boolean DFS(char[][] board, String word, int i, int j, int k, boolean[][] visited) {
    visited[i][j] = true;
    if (k >= word.length())
        return true;
    for (int z = 0; z < 4; z++) {
        if (isValid(board, i + dirx[z], j + diry[z], visited)) {
            if (word.charAt(k) == board[i + dirx[z]][j + diry[z]]) {

                return DFS(board, word, i + dirx[z], j + diry[z], k + 1, visited);
            }

        }
    }
    return false;
}

暫無
暫無

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

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