我正在努力实现在随机5x5的字母板中找到所有单词的解决方案。目前,它返回了一些单词,但不是完整的列表。我很确定我的问题存在于findWords方法和我的for循环中,但我不知道如何使if语句继续在所有8个方向上遍历。
import java.io.File;
import java.util.*;
public class RandomWordGame {
private static char[][] board = new char[5][5];
private static Random r = new Random();
private static ArrayList<String> dictionary = new ArrayList<String>();
private static char[][] createBoard()
{
for (int i=0; i<board.length; i++)
{
for (int j=0; j<board.length; j++)
{
board[i][j] = (char) (r.nextInt(26) + 'a');
System.out.print(board[i][j]);
}
System.out.println("");
}
System.out.println();
return board;
}
public static ArrayList<String> solver(char[][] board)
{
if(board == null)
System.out.println("Board cannot be empty");
ArrayList<String> words = new ArrayList<String>();
for(int i=0; i<board.length; i++)
{
for(int j=0; j<board[0].length; j++)
{
findWords(i, j, board[i][j] + "");
}
}
return words;
}
public static void findWords(int i, int j, String currWord)
{
try
{
Scanner inputStream = new Scanner(new File("./dictionary.txt"));
while(inputStream.hasNext())
{
dictionary.add(inputStream.nextLine());
}
inputStream.close();
}catch(Exception e){
e.printStackTrace();
}
for(i=0; i>=0 && i<board.length; i++)
{
for(j=0; j>=0; j++)
{
currWord += board[i][j];
if(currWord.length()>5)
return;
if(dictionary.contains(currWord))
System.out.println(currWord);
}
}
}
public static void main(String[] args)
{
board = createBoard();
ArrayList<String> validWords = RandomWordGame.solver(board);
for(String word : validWords)
System.out.println(word);
}
}
这段代码有一些有趣的地方。首先,你总是从你的求解器方法返回一个空的ArrayList,但这并不是杀死你的原因,因为你正在打印来自findWords的每个结果。
问题是findWords方法只是不断地从拼图的左上角开始添加字母。
//i=0 and j=0 means it will always start at the top left tile
for(i=0; i>=0 && i<board.length; i++)
{
for(j=0; j>=0; j++)
{
//currWord is never reset, so it just keeps getting longer
currWord += board[i][j];
if(currWord.length()>5)
return;
if(dictionary.contains(currWord))
System.out.println(currWord);
}
}
现在你只能找到以你选择的贴图开头的单词,其余的字母是按照从左上角开始的顺序被选中的。
我建议花点时间用笔和纸,深入了解二维数组索引和它们在网格中的位置之间的关系。
使用DFS方法的Java实现
import java.util.Arrays;
public class WordBoggle {
static int[] dirx = { -1, 0, 0, 1 };
static int[] diry = { 0, -1, 1, 0 };
public static void main(String[] args) {
char[][] board = { { 'A', 'B', 'C', 'E' }, { 'S', 'F', 'C', 'S' }, { 'A', 'D', 'E', 'E' } };
String word = "ABFSADEESCCEA";
System.out.println(exist(board, word));
}
static boolean exist(char[][] board, String word) {
if (board == null || board.length == 0 || word == null || word.isEmpty())
return false;
boolean[][] visited = new boolean[board.length][board[0].length];
for (int i = 0; i < board.length; i++) {
resetVisited(visited);
for (int j = 0; j < board[0].length; j++) {
if (board[i][j] == word.charAt(i)) {
return DFS(board, word, i, j, 1, visited);
}
}
}
return false;
}
static void resetVisited(boolean[][] visited) {
for (int l = 0; l < visited.length; l++) {
Arrays.fill(visited[l], false);
}
}
static boolean DFS(char[][] board, String word, int i, int j, int k, boolean[][] visited) {
visited[i][j] = true;
if (k >= word.length())
return true;
for (int z = 0; z < 4; z++) {
if (isValid(board, i + dirx[z], j + diry[z], visited)) {
if (word.charAt(k) == board[i + dirx[z]][j + diry[z]]) {
return DFS(board, word, i + dirx[z], j + diry[z], k + 1, visited);
}
}
}
return false;
}
static boolean isValid(char[][] board, int i, int j, boolean[][] visited) {
return (i >= 0 && i < board.length && j >= 0 && j < board[0].length && !visited[i][j]);
}
}