Boggle求解器实现

时间:2016-11-12 01:49:45

标签: java file dictionary arraylist boggle

我正在努力实现一个解决方案,以便在随机的5x5字母板中查找所有单词。目前它正在返回几个字,但不是几乎完整列表。我很确定我的问题存在于带有for循环的findWords方法中,但是我无法弄清楚使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);
    }
}

2 个答案:

答案 0 :(得分:0)

这段代码有一些有趣的东西。首先,你总是从你的求解器方法返回一个空的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);
    }
}

现在你只是找到以你选择的瓷砖开头的单词,其余的单词是从左上角开始添加到拼图中的相同顺序。

我建议花一些时间用铅笔和纸,并深入了解二维数组索引之间的关系及其在网格中的位置。

答案 1 :(得分:0)

使用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]);
    }
}