【问题标题】:Java wordsearch Char arraysJava wordsearch Char 数组
【发布时间】:2014-02-08 03:31:33
【问题描述】:

我已经在这个单词搜索项目上苦苦挣扎了几天,只是想让水平搜索工作。它适用于所有 8 个可能的方向(水平、垂直、对角线)。这是我当前的代码。

现在我只担心水平,因为我怀疑如果我把比较直接下来,那么剩下的会更简单。

我打算编写代码来查找包含在板数组中的单词,并将这些字符输出到另一个与板数组大小相同的数组(因此,输出数组是板数组的解决方案)。

到目前为止,我的代码所做的只是遍历整个板子,然后检查它是否与单词列表的第一个字符匹配,如果匹配,则将该字符分配到输出数组上,最后打印出来输出到控制台供用户查看。

我的问题是,我怎样才能转发我的搜索以迭代单词列表?我的方法错了吗?例如,如果 board char 与 wordlist 中的 char 匹配,则继续沿指定方向(在我的情况下,我担心水平方向)并找到单词。

此外,“过滤器”方法旨在处理异常OutofBounds,以防搜索失败。

欢迎任何想法或方法。

【问题讨论】:

  • 您是否有一个应该在网格中某处找到的单词的简短列表,或者这是针对非常大的有效单词字典的“查找所有有效单词”?
  • @TeresaCarrigan 是的,我有一个简短的单词列表,应该在网格上找到一些单词。网格和单词列表都在同一个文件中。
  • 请不要在您的问题解决后删除您的问题。相反,请标记适当的答案。
  • 删除了我的代码,因为我不希望人们使用我的代码。这个问题是出于想法目的而提出的。已标记答案,不便之处敬请见谅。

标签: java arrays char


【解决方案1】:

你可以试试这个水平搜索 字符串字=“”; for (String keyWord : words) {

        // STEP1: Find *************IF ******************* The word is in
        // the Array
        // CODE HERE
        boolean found = false;
        for (int i = 0; i < puzzle.length; i++) {
            String rowString = "";
            for (int j = 0; j < puzzle[i].length; j++) {
                rowString += puzzle[i][j];
                if (rowString.contains(keyWord) && !found) {

                    System.out.println(keyWord);
                    int index = rowString.indexOf(keyWord);
                    rowString.indexOf(keyWord);
                    // int length = keyWord.length();
                    for (int ii = 0; ii < keyWord.length(); ii++) {
                        solutionArray[i][index + ii] = keyWord.charAt(ii);
                        rowString += puzzle[i][j];
                        System.out.println();
                        // length--;
                    }
                    found = true;
                }

            }
        }

【讨论】:

    【解决方案2】:

    这是一个在网格中搜索不同方向的单词的示例。我已经实现了其中的三个,其中三个留给你完成。在我个人的偏好中,我会为 wordList 使用字符串数组而不是锯齿状数组,但我选择了 OP 的实现方式。我使用 4 x 4 网格和 3 个单词的列表制作了一个简单的版本。请注意,我在输出板上调用了 fillWithSpaces()。这对于格式化至关重要。

    我有一个名为“board.txt”的文本文件

    dcat
    aoiq
    eigk
    snur
    

    和一个文本文件“words.txt”

    dog
    cat
    runs
    

    这是程序的输出:

    DCAT
    -O--
    --G-
    SNUR
    

    我的策略是在板上搜索单词的第一个字母。找到它后,我会更新静态字段 foundRow 和 foundColumn。当我使用不同的词时,我会更新静态字段 currentWord。当我找到匹配的字母时,我有六种不同的方法,checkForwards() checkBackwards() 等等。 (还有其他方法可以做到这一点,但我试图使示例尽可能清晰。

    这里是向后检查的方法。由于我已经知道第一个字母匹配,我从第二个(索引 1)开始。对于每个新字符,在比较值之前,我会检查它是否会出现在板上。 (也有更好的方法来做到这一点)。如果有什么失败,我会回来。如果所有字符都匹配,我一次复制每个字符。

    static void checkBackwards()
    {
        for(int i = 1; i < wordList[currentWord].length; i++)
        {
            if(foundColumn - i < 0) return;
            if(wordList[currentWord][i] != board[foundRow][foundColumn - i]) return;
        }
        //if we got to here, update the output
        for(int i = 0; i < wordList[currentWord].length; i++)
        {
            output[foundRow][foundColumn - i] = wordList[currentWord][i];
        }
        return;
    }
    

    这里是源代码:

    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.Scanner;
    
    public class Wordsearch 
    {
        static Scanner input;
        static char[][] wordList;
        static char[][] board;
        static char[][] output;
    
        static int foundRow;
        static int foundColumn;
        static int currentWord;
    
        public static void main(String[] args) throws FileNotFoundException
        {
            File wordInput = new File("words.txt");
            File boardInput = new File("board.txt");
            if(!wordInput.exists() || !boardInput.exists())
            {
                System.out.println("Files do not exist.");
                System.exit(1);
            }
    
            wordList = new char[3][];   //word list matrix 
            board = new char[4][4]; //board or grid matrix
            output= new char[4][4]; //solved puzzle 
    
            fillWithSpaces(output);
    
            input = new Scanner(wordInput);
            for(int i = 0; i < wordList.length; i++)
            {
                wordList[i] = input.nextLine().toUpperCase().toCharArray();
            }
    
            input = new Scanner(boardInput);
            for(int i = 0; i < board[0].length; i++)
            {
                board[i] = input.nextLine().toUpperCase().toCharArray();
            }
    
            for(int i = 0; i < wordList.length; i++)
            {
                currentWord = i;
                if(findFirstLetter())
                {
                    checkEachDirection();
                }
            }
    
            print(output);
        }
    
        static boolean findFirstLetter()
        {
            for(int r = 0; r < board.length; r++)
            {
                for(int c = 0; c < board.length; c++)
                {
                    if(wordList[currentWord][0] == board[r][c])
                    {
                        foundRow = r;
                        foundColumn = c;
                        return true;
                    }
                }
            }
            return false;
        }
    
        static void checkEachDirection()
        {
            checkForwards();
            checkBackwards();
            //checkUp();
            //checkDown();
            checkDiagonalDown();
            //checkDiagonalUp();
        }
    
        static void checkForwards()
        {
            for(int i = 1; i < wordList[currentWord].length; i++)
            {
                if(foundColumn + i > board.length - 1) return;
                if(wordList[currentWord][i] != board[foundRow][foundColumn + i]) return;
            }
            //if we got to here, update the output
            for(int i = 0; i < wordList[currentWord].length; i++)
            {
                output[foundRow][foundColumn + i] = wordList[currentWord][i];
            }
            return;
        }
    
        static void checkBackwards()
        {
            for(int i = 1; i < wordList[currentWord].length; i++)
            {
                if(foundColumn - i < 0) return;
                if(wordList[currentWord][i] != board[foundRow][foundColumn - i]) return;
            }
            //if we got to here, update the output
            for(int i = 0; i < wordList[currentWord].length; i++)
            {
                output[foundRow][foundColumn - i] = wordList[currentWord][i];
            }
            return;
        }
    
        static void checkDiagonalDown()
        {
            for(int i = 1; i < wordList[currentWord].length; i++)
            {
                if(foundColumn + i > board.length - 1) return;
                if(foundRow + i > board.length - 1) return;
                if(wordList[currentWord][i] != board[foundRow + i][foundColumn + i]) return;
            }
            //if we got to here, update the output
            for(int i = 0; i < wordList[currentWord].length; i++)
            {
                output[foundRow + i][foundColumn + i] = wordList[currentWord][i];
            }
            return;
        }
    
        static void print(char[][] board)
        {
            for(int i = 0; i < board.length; i++)
            {
                for(int j = 0; j < board.length; j++)
                {
                    System.out.print(board[i][j]);
                }
                System.out.println();
            }
            System.out.println();
        }
    
        static void fillWithSpaces(char[][] board)
        {
            for(int i = 0; i < board.length; i++)
            {
                for(int j = 0; j < board.length; j++)
                {
                    board[i][j] = '-';
                }
            }
        }
    }
    

    【讨论】:

    • 我试图实现这个确切的方法,只是不知道从哪里开始。非常感谢。
    【解决方案3】:

    考虑以下程序:

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-10-03
      • 2011-10-04
      • 2013-04-05
      • 2015-12-06
      • 2019-04-15
      • 2012-07-25
      • 2013-12-11
      相关资源
      最近更新 更多