考慮下面的程序:
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);
}
}
}
}
}
該板是2維字符數組和您要搜索的單詞列表稱爲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
你有那都應該是在網格中發現某處,或者這是一個「查找所有有效的話」對一個非常大的字典的字的簡短列表有效的單詞? –
@TeresaCarrigan是的,我有一個應該在網格上找到的單詞的簡短列表。網格和單詞列表都在同一個文件中。 – fzxt
解決問題後,請不要刪除您的問題。相反,請標記適當的答案。 –