這裏是在Java中的MxN板上的單詞搜索的例子。單詞可以放在任何方向上:水平(從左到右,從右到左),垂直(從上到下,從下到上),對角線(任何方向)。
import java.util.InputMismatchException;
/**
* Created on 1/21/17.
*/
public class WordSearchPuzzle {
private int currentChar; // Index of current character searched in word array
private char word[]; //Searched word
public boolean wordExists(char board[][], String word) {
if (word.isEmpty()) {
throw new InputMismatchException("Searched word can not be empty!");
}
this.word = new char[word.length()];
currentChar = 0;
for (int a = 0; a < word.length(); a++) {
word.getChars(0, word.length(), this.word, 0);
}
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[i].length; j++) {
if (this.word.length != 0 && board[i][j] == this.word[currentChar]) {
if (this.word.length == 1) {
return true;
}
if (letterExists(board, i, j, this.word[++currentChar], "N")) {
return true;
}
currentChar = 0;
if (letterExists(board, i, j, this.word[++currentChar], "NE")) {
return true;
}
currentChar = 0;
if (letterExists(board, i, j, this.word[++currentChar], "E")) {
return true;
}
currentChar = 0;
if (letterExists(board, i, j, this.word[++currentChar], "SE")) {
return true;
}
currentChar = 0;
if (letterExists(board, i, j, this.word[++currentChar], "S")) {
return true;
}
currentChar = 0;
if (letterExists(board, i, j, this.word[++currentChar], "SW")) {
return true;
}
currentChar = 0;
if (letterExists(board, i, j, this.word[++currentChar], "W")) {
return true;
}
currentChar = 0;
if (letterExists(board, i, j, this.word[++currentChar], "NW")) {
return true;
}
//otherwise continue to search from first letter of given word,
//starting with some another position in the board
currentChar = 0;
continue;
}
}
}
return false;
}
public boolean letterExists(char board[][], int i, int j, char letter, String direction) {
if (i < 0 || i > board.length || j < 0 || j > board.length) {
throw new IndexOutOfBoundsException("Unable to search for letter " + letter + " with coordinates (" + i + ", " + j + ")");
}
currentChar++;//advance search character to next letter in word
if (i - 1 >= 0 && board[i - 1][j] == letter && direction.equals("N")) { //search N
if (currentChar == word.length)
return true;
return letterExists(board, i - 1, j, word[currentChar], "N");
} else if (i - 1 >= 0 && j + 1 < board[i].length && board[i - 1][j + 1] == letter && direction.equals("NE")) {//search NE
if (currentChar == word.length)
return true;
return letterExists(board, i - 1, j + 1, word[currentChar], "NE");
} else if (j + 1 < board[i].length && board[i][j + 1] == letter && direction.equals("E")) { //search E
if (currentChar == word.length)
return true;
return letterExists(board, i, j + 1, word[currentChar], "E");
} else if (i + 1 < board.length && j + 1 < board[i + 1].length && board[i + 1][j + 1] == letter && direction.equals("SE")) { //search SE
if (currentChar == word.length)
return true;
return letterExists(board, i + 1, j + 1, word[currentChar], "SE");
} else if (i + 1 < board.length && board[i + 1][j] == letter && direction.equals("S")) {//search S
if (currentChar == word.length)
return true;
return letterExists(board, i + 1, j, word[currentChar], "S");
} else if (i + 1 < board.length && j - 1 >= 0 && board[i + 1][j - 1] == letter && direction.equals("SW")) { //search SW
if (currentChar == word.length)
return true;
return letterExists(board, i + 1, j - 1, word[currentChar], "SW");
} else if (j - 1 >= 0 && board[i][j - 1] == letter && direction.equals("W")) { //search W
if (currentChar == word.length)
return true;
return letterExists(board, i, j - 1, word[currentChar], "W");
} else if (j - 1 >= 0 && i - 1 >= 0 && board[i - 1][j - 1] == letter && direction.equals("NW")) { //search NW
if (currentChar == word.length)
return true;
return letterExists(board, i - 1, j - 1, word[currentChar], "NW");
}
return false;
}
public static void main(String[] args) {
char row1[] = {'a', 'm', 'e', 'r' };
char row2[] = {'m', 'i', 'z', 'g' };
char row3[] = {'k', 'l', 'b', 'f' };
char row4[] = {'s', 't', 'o', 'c' };
char row5[] = {'b', 'a', 'y', 'a' };
char board[][] = new char[5][4];
board[0] = row1;
board[1] = row2;
board[2] = row3;
board[3] = row4;
board[4] = row5;
WordSearchPuzzle puzzle = new WordSearchPuzzle();
if (puzzle.wordExists(board, "ezb")) {
System.out.println("Word exists!");
} else {
System.out.println("Word doesn't exist!");
}
}
}
「word」定義在哪裏? –
在上面的一些代碼中,我沒有包括,但基本上代碼要求用戶輸入他們想要搜索的單詞,然後將它們的輸入存儲在單詞變量中。 – user3113722