【问题标题】:How to do a recursive search for a word in the Boggle game board?如何在 Boggle 游戏板上递归搜索单词?
【发布时间】:2014-04-15 05:56:58
【问题描述】:

有人可以帮助我使用伪代码,甚至是描述在 Boggle 板上递归搜索单词的递归公式,这样我就可以开始了吗?

【问题讨论】:

  • Regular Boggle 还是 Super Boggle?
  • 普通的boggle游戏哈哈我什至不知道有一个超级boggle
  • 我在这里发布了一个 Java 实现:stackoverflow.com/a/52824972/4523235

标签: java recursion boggle


【解决方案1】:

假设您在某处有一个可用的单词列表,可能存储在 Trie 数据结构中(我已经使用 cmets 创建了一个工作 Trie,以提高其效率 here)。

一旦你有一个 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. */


}

【讨论】:

    【解决方案2】:

    为了存储有效词,带有检查方法的数据结构被赋予了一些有效词的字符串前缀,并且被赋予了字符串一个有效词是必需的,例如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
    

    【讨论】:

      【解决方案3】:

      使用 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;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-01-21
        相关资源
        最近更新 更多