考虑以下程序:
import java.util.ArrayList;
public class WordSearch {
static char[][] board;
static int board_x, board_y;
static ArrayList<String> search_words;
public static void main(String args[])
{
board = new char[][]{
{ 's', 't', 'a', 'c', 'k' },
{ 'x', 'f', 'l', 'o', 'w' },
{ 'x', 'x', 'x', 'v', 'x' },
{ 'x', 'x', 'x', 'e', 'x' },
{ 'x', 'x', 'x', 'r', 'x' },
};
// You could also get these from board.size, etc
board_x = 5;
board_y = 5;
search_words = new ArrayList<String>();
search_words.add("stack");
search_words.add("over");
search_words.add("flow");
search_words.add("not");
for(String word : search_words){
find(word);
}
}
public static void find(String word)
{
// Search for the word laid out horizontally
for(int r=0; r<board_y; r++){
for(int c=0; c<=(board_x - word.length()); c++){
// The pair (r,c) will always be where we start checking from
boolean match = true;
for(int i=0; i<word.length(); i++){
if(board[r][c + i] != word.charAt(i)){
match = false;
System.out.format(" '%s' not found starting at (%d,%d) -- first failure at %d\n", word, r, c, i);
break;
}
}
if(match){
System.out.format("Found match (horizontal) for '%s' starting at (%d,%d)\n", word, r, c);
}
}
}
}
}
板是一个二维字符数组,您要搜索的单词列表是一个名为 search_words 的 ArrayList。
在对板子和search_words 列表进行一些简单的示例初始化之后,它会遍历列表中的单词,如果它水平放置,则搜索每个单词。
这个想法可以通过一些调整扩展到垂直或对角搜索。
这里的逻辑是您应该从示例程序中获取的内容,而不一定是结构。如果我这样做是为了任何严肃的事情,我可能会有一个 Board 类,可能有一个 .find(word) 方法。
最后,详细的输出是:
Found match (horizontal) for 'stack' starting at (0,0)
'stack' not found starting at (1,0) -- first failure at 0
'stack' not found starting at (2,0) -- first failure at 0
'stack' not found starting at (3,0) -- first failure at 0
'stack' not found starting at (4,0) -- first failure at 0
'over' not found starting at (0,0) -- first failure at 0
'over' not found starting at (0,1) -- first failure at 0
'over' not found starting at (1,0) -- first failure at 0
'over' not found starting at (1,1) -- first failure at 0
'over' not found starting at (2,0) -- first failure at 0
'over' not found starting at (2,1) -- first failure at 0
'over' not found starting at (3,0) -- first failure at 0
'over' not found starting at (3,1) -- first failure at 0
'over' not found starting at (4,0) -- first failure at 0
'over' not found starting at (4,1) -- first failure at 0
'flow' not found starting at (0,0) -- first failure at 0
'flow' not found starting at (0,1) -- first failure at 0
'flow' not found starting at (1,0) -- first failure at 0
Found match (horizontal) for 'flow' starting at (1,1)
'flow' not found starting at (2,0) -- first failure at 0
'flow' not found starting at (2,1) -- first failure at 0
'flow' not found starting at (3,0) -- first failure at 0
'flow' not found starting at (3,1) -- first failure at 0
'flow' not found starting at (4,0) -- first failure at 0
'flow' not found starting at (4,1) -- first failure at 0
'not' not found starting at (0,0) -- first failure at 0
'not' not found starting at (0,1) -- first failure at 0
'not' not found starting at (0,2) -- first failure at 0
'not' not found starting at (1,0) -- first failure at 0
'not' not found starting at (1,1) -- first failure at 0
'not' not found starting at (1,2) -- first failure at 0
'not' not found starting at (2,0) -- first failure at 0
'not' not found starting at (2,1) -- first failure at 0
'not' not found starting at (2,2) -- first failure at 0
'not' not found starting at (3,0) -- first failure at 0
'not' not found starting at (3,1) -- first failure at 0
'not' not found starting at (3,2) -- first failure at 0
'not' not found starting at (4,0) -- first failure at 0
'not' not found starting at (4,1) -- first failure at 0
'not' not found starting at (4,2) -- first failure at 0