[英]Searching multiple directions of a 2d array at once
我正在制作一個難以置信的應用程序,目前在檢查網格上是否存在單詞的搜索方法方面存在問題。 如果我相鄰有相同字母的重復,其中一個不會導致該單詞,但另一個會導致該單詞,則該方法返回 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?
問題是letterIndex
中的 return 語句。 根據您的代碼編寫方式,調用wordCheck
后letterIndex
將始終返回,但似乎您希望它僅在wordCheck
說它找到一個單詞時返回,否則您想繼續查找。 這有幫助嗎?
您可以做的是,將您的搜索分成幾種不同的方法。
在您的wordCheck
方法中,只需檢查是否有任何下一個方向指向目標。
例子:
public static boolean wordCheck(String target, int row, int col, char[][] board, int index, char[] temp) {
return wordCheckDown(target, row, col, board, index, temp)
|| wordCheckRight(target, row, col, board, index, temp)
|| wordCheckUp(target, row, col, board, index, temp)
|| wordCheckLeft(target, row, col, board, index, temp)
|| wordCheckUpperRight(target, row, col, board, index, temp)
|| wordCheckLowerLeft(target, row, col, board, index, temp)
|| wordCheckUpperLeft(target, row, col, board, index, temp)
|| wordCheckLowerRight(target, row, col, board, index, temp);
}
在這種方法中,它將在所有方向上檢查單詞。 表達方式是“短路”。 這意味着,如果在我的示例中wordCheckDown
方法返回 true,則不會評估其他方法。 如果wordCheckDown
返回 false,則評估wordCheckRight
等等。
然后在方向方法中檢查下一個char
是否適合並遞歸調用wordCheck
方法,如下面的wordCheckLowerRight
方法示例:
private static boolean wordCheckLowerRight(String target, int row, int col, char[][] board, int index, char[] temp) {
//Checks every recursive run if the word has been found
if (target.equals(String.valueOf(temp))) {
return true;
}
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;
}
另外你的letterIndex
方法也有問題。 在檢查與單詞的起始字母匹配的第一個char
后停止。 繼續搜索整個字段,而沒有找到該詞。
例如,您可以為此使用boolean
變量,如下例所示:
public static boolean letterIndex(String target, char[][] board) {
if (target.length() < 1) {
System.out.println("Invalid, please enter a word");
return false;
}
boolean foundWord = false;
for (int i = 0; i < board.length && !foundWord; i++) {
for (int j = 0; j < board[0].length && !foundWord; j++) {
if (board[i][j] == target.charAt(0)) {
char[] temp = new char[target.length()];
temp[0] = target.charAt(0);
foundWord = wordCheck(target, i, j, board, 1, temp);
}
}
}
return foundWord;
}
在此示例中, for
循環檢查是否滿足數組的索引,以及是否已經找到該單詞。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.