首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Java wordsearch Char数组

Java wordsearch Char数组
EN

Stack Overflow用户
提问于 2014-01-19 03:45:25
回答 3查看 16.3K关注 0票数 2

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

现在我只担心水平方向,因为我怀疑如果我得到正确的比较,剩下的事情就会更简单。

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

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

我的问题是,我如何转发我的搜索来迭代单词列表呢?我的方法是错的吗?例如,如果board char与单词列表中的char匹配,则在指定的方向上继续(在我的例子中,我担心的是水平方向)并找到单词。

此外,方法'filter‘是为了在搜索失败的情况下处理exceptionOutofBounds。

欢迎任何想法或方法。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2014-01-19 04:50:54

下面是一个在网格中搜索不同方向的单词的示例。我已经实现了其中的三个,并将其中的三个留给您来完成。在我个人的偏好中,我会对wordList使用字符串数组而不是锯齿数组,但是我选择了OP选择的实现。我做了一个简单的版本,使用一个4x4的网格和一个包含3个单词的列表。注意,我在输出板上调用了fillWithSpaces()。这对于格式化非常关键。

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

代码语言:javascript
复制
dcat
aoiq
eigk
snur

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

代码语言:javascript
复制
dog
cat
runs

下面是程序的输出:

代码语言:javascript
复制
DCAT
-O--
--G-
SNUR

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

下面是向后检查的方法。因为我已经知道第一个字母匹配,所以我从第二个(索引1)开始。对于每个新字符,在比较这些值之前,我会检查它是否会出现在电路板上。(还有一种更好的方法可以做到这一点)。如果任何东西都失败了,我会返回。如果所有字符都匹配,我会一次复制一个字符。

代码语言:javascript
复制
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;
}

下面是源代码:

代码语言:javascript
复制
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] = '-';
            }
        }
    }
}
票数 7
EN

Stack Overflow用户

发布于 2014-01-19 04:15:52

考虑以下程序:

代码语言:javascript
复制
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)方法。

最后,详细输出为:

代码语言:javascript
复制
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
票数 2
EN

Stack Overflow用户

发布于 2015-12-09 04:57:45

您可以尝试水平搜索String word = "";for (String keyWord : words) {

代码语言:javascript
复制
        // 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;
                }

            }
        }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21208997

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档