繁体   English   中英

Java-通过2D字符数组搜索单词?

[英]Java - Searching through a 2D array of characters for words?

因此,我一直在创建一个程序,当用户输入一个单词时,该程序将播放boggle,它会通过二维字符数组(boggle board)搜索第一个字母,如果找到它,它将搜索周围的所有字符。 但是它只执行一次,并在输入单词的第一个字母周围打印字母。 如何添加一个函数或添加到当前的函数中,以使其能够继续通过输入的单词搜索字母,直到找到或找不到完整的单词? 我是否必须继续调用checkSurrounding()函数?

 private static void checkForFirstLetter(char[][] boggle, String word) {
    for(int i = 0; i < 5; i++) {
        for(int j = 0; j < 5; j++) {
            if(boggle[i][j] == word.charAt(0)) {
                System.out.println("\nFound " + boggle[i][j] + " at (" + (j+1) + ", " + (i+1) + ")!");
                checkSurrounding(boggle, j, i);
            }
        }
    }
}


private static void checkSurrounding(char[][] boggle, int x, int y) {
    for(int dx = -1; dx <= 1; dx++) {
        if ((x + dx >= 0) && (x + dx < boggle.length)) {
            for(int dy = -1; dy <= 1; dy++) {
                if ((y + dy >= 0) && (y + dy < boggle[x + dx].length) && (!(dx == 0 && dy == 0))) {
                    System.out.print(boggle[y + dy][x + dx]);
                }
            }
        }
    } 
}

是的,一旦您发现一个以单词开头的字符,我将对checkSurrounding进行递归。 在该函数中,我将包括单词本身的参数。 在检查单词中紧随其后的字符时,如果我找到了位于单词索引0处的字符,那么我会递归执行,但只会查看单词的后n-1个字符,而忽略刚才找到的字符。 如果要使用此实现,则需要跟踪已经遇到的字符,以便忽略已找到的字符。

实际上,您几乎已经拥有了匹配整个单词所需的一切。 关键是使用递归尝试将部分匹配项扩展到当前匹配字符的8个邻居中。

您必须要注意的一件事是在比赛中不要多次考虑板上的字母。 诀窍是通过在检查邻居之前将其设置为空白来清除当前字母-这可以确保它不会成为任何将来匹配项的一部分。 一旦考虑了所有邻居,便将字母重新设置为其原始值。

下面的代码仅统计发现了多少个不同的匹配项。 跟踪每场比赛的字母位置真的很不错,但这有点棘手。

这主要是您的代码,还有一些附加内容:

static int checkForFirstLetter(char[][] board, String word)
{
  int count = 0;
  for (int x = 0; x < board.length; x++) {
    for (int y = 0; y < board.length; y++) {
      if (board[x][y] == word.charAt(0)) {
        count += checkSurrounding(board, x, y, word, 0);
      }
    }
  }
  return count;
}

static int checkSurrounding(char[][] board, int x, int y, String word, int i)
{
  if (i == word.length()) 
    return 1;

  if (board[x][y] != word.charAt(i)) 
    return 0;

  int count = 0;

  // Clear the current position so we don't consider it again in a match
  board[x][y] = 0;
  for (int dx = -1; dx <= 1; dx++) {
    if ((x + dx >= 0) && (x + dx < board.length)) {
      for (int dy = -1; dy <= 1; dy++) {
        if ((y + dy >= 0) && (y + dy < board[x + dx].length) && (!(dx == 0 && dy == 0))) {
          count += checkSurrounding(board, x + dx, y + dy, word, i + 1);
        }
      }
    }
  }
  // Reinstate the character at the current position, which has to be at pos i in word
  board[x][y] = word.charAt(i);

  return count;
}

测试:

public static void main(String[] args)
{
  char[][] board = {
      {'o', 'x', 'o', 'x', 'o'},
      {'x', 'o', 'x', 'o', 'x'},
      {'o', 'x', 'o', 'x', 'o'},
      {'x', 'o', 'x', 'o', 'x'},
      {'o', 'x', 'o', 'x', 'o'}};

  for(char[] b : board)
    System.out.println(new String(b).replaceAll(".", "$0 "));

  int count = checkForFirstLetter(board, "oxo");
  System.out.printf("\nFound %d word(s)\n", count);
}

输出:

o x o x o 
x o x o x 
o x o x o 
x o x o x 
o x o x o 

Found 604 word(s)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM