java - 一次搜索二维数组的多个方向
问题描述
我正在制作一个难以置信的应用程序,目前在检查网格上是否存在单词的搜索方法方面存在问题。如果我相邻有相同字母的重复,其中一个不会导致该单词,但另一个会导致该单词,则该方法返回 false。
例如,good 将返回 false,因为将首先检查左下角的 O 而不是右上角的 O,这将导致正确答案。
public boolean letterIndex(String target, char[][] board) {
if(target.length() < 1) {
System.out.println("Invalid, please enter a word");
}
for (int i = 0; i < board.length; i++) {//For loop iteratively traverses the array
for (int j = 0; j < board[0].length; j++) {
if (board[i][j] == target.charAt(0)) {//Checks for where on the board the first letter of the target word appears
char[] temp = new char[target.length()];//Creates a temporary array that stores the target string as a character array
temp[0] = target.charAt(0);//Sets the first index of the array as the first letter of the word
return wordCheck(target, i, j, board, 1, temp);//Call wordcheck method
}
}
}
return false;
}
//Checks all the directions of the position passed by letterIndex recursively until either the word has been found or all directions return false
public static boolean wordCheck(String target, int row, int col, char[][] board, int index, char[] temp) {
if (target.equals(String.valueOf(temp))) {//Checks every recursive run if the word has been found
return true;
}
if (!target.equals(String.valueOf(temp))) {
if (row + 1 < board.length) {//Check down
if (board[row + 1][col] == target.charAt(index)) {
temp[index] = board[row + 1][col];//Add the letter to the temp array
index++;
return wordCheck(target, row + 1, col, board, index, temp);
}
}
if (col + 1 < board[0].length) {//Check right
if (board[row][col + 1] == target.charAt(index)) {
temp[index] = board[row][col + 1];//Add the letter to the temp array
index++;
return wordCheck(target, row, col + 1, board, index, temp);
}
}
if (row - 1 >= 0) {//Check up
if (board[row - 1][col] == target.charAt(index)) {
temp[index] = board[row - 1][col];//Add the letter to the temp array
index++;
return wordCheck(target, row - 1, col, board, index, temp);
}
}
if (col - 1 >= 0) {//Check left
if (board[row][col - 1] == target.charAt(index)) {
temp[index] = board[row][col - 1];//Add the letter to the temp array
index++;
return wordCheck(target, row, col - 1, board, index, temp);
}
}
if (row - 1 >= 0 && col + 1 < board[0].length) {//Check upperright
if (board[row - 1][col + 1] == target.charAt(index)) {
temp[index] = board[row - 1][col + 1];//Add the letter to the temp array
index++;
return wordCheck(target, row - 1, col + 1, board, index, temp);
}
}
if (row + 1 < board.length && col - 1 >= 0) {//Check lowerleft
if (board[row + 1][col - 1] == target.charAt(index)) {
temp[index] = board[row + 1][col - 1];//Add the letter to the temp array
index++;
return wordCheck(target, row + 1, col - 1, board, index, temp);
}
}
if (row - 1 >= 0 && col - 1 >= 0) {//Check upperleft
if (board[row - 1][col - 1] == target.charAt(index)) {
temp[index] = board[row - 1][col - 1];//Add the letter to the temp array
index++;
return wordCheck(target, row - 1, col - 1, board, index, temp);
}
}
if (row + 1 < board.length && col + 1 < board[0].length) {
if (board[row + 1][col + 1] == target.charAt(index)) {//Check lowerright
temp[index] = board[row + 1][col + 1];//Add the letter to the temp array
index++;
return wordCheck(target, row + 1, col + 1, board, index, temp);
}
}
}
return false; //If the next letter isn't in any direction, the word doesn't exist
}
我知道问题在于 if 语句的顺序,有没有办法让程序检查所有方面?此外,目前它只检查字母的第一个索引(在 letterArray 处)有没有办法让它为字母的所有索引运行 wordCheck?
解决方案
问题是 .return 中的返回语句letterIndex
。你的代码编写方式,letterIndex
总是在调用后返回wordCheck
,但似乎你希望它只在找到一个单词时才返回wordCheck
,否则你想继续查找。这有帮助吗?