다음은 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