首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我的Boggle程序找不到所有有效的单词- Java

我的Boggle程序找不到所有有效的单词- Java
EN

Stack Overflow用户
提问于 2012-10-17 03:19:03
回答 1查看 1.4K关注 0票数 0

因此,我正在用java编写一个Boggle程序,但我很难让它找到每一个可能的单词。

它几乎能找到所有的东西,但我真搞不懂为什么它找不到所有的东西。

代码语言:javascript
复制
private void findWords( TreeSet<String> foundWords, String word, Tile t ){
    int i=t.getRow();
    int j=t.getCol();

    //Make sure the tile isn't visited
    if(visited[i][j]) return;

    //Set the current tile to visited
    visited[i][j]=true;

    //Decide what the current word is
    if(t.getLetter().equalsIgnoreCase("q")) word=word+"qu";
    else word=word+t.getLetter();

    //If the string is a word
    if(Boggle.dictionary.search(word)==1){
        //If the word length is greater than or equal to the prefix length
        if(word.length()>=Dictionary.prefixLength){
            //If the word has not already been found
            if(!foundWords.contains(word)){
                //Add the word to the found list
                foundWords.add(word);   
            }
        }
    }

    //Recurse through all neighbor tiles
    for(int curRow=0; curRow<=nRows; curRow++){
        for(int curCol=0; curCol<=nCols; curCol++){
            //Make sure it is not out of bounds
            if((i+curRow<nRows)&&(j+curCol<nCols)){
                findWords(foundWords, word, board[i + curRow][j + curCol]); 
                findWords(foundWords, word, board[i - curRow][j - curCol]); 

                findWords(foundWords, word, board[i + curRow][curCol]); 
                findWords(foundWords, word, board[i - curRow][curCol]); 

                findWords(foundWords, word, board[curRow][j + curCol]); 
                findWords(foundWords, word, board[curRow][j - curCol]); 

                findWords(foundWords, word, board[i + curRow][j - curCol]);
                findWords(foundWords, word, board[i - curRow][j + curCol]);
            }
        }
    }

    //Reset the tile to be not visited
    visited[i][j] = false;
}

这就是我们讨论的方法;它递归地在Boggle板上查找单词。

有人知道为什么它只能找到75%的单词吗?

我可以附加更多的代码,如果需要的话。

EN

回答 1

Stack Overflow用户

发布于 2012-10-17 03:21:46

我建议写一些测试用例-我总是发现这样做可以立即发现问题,或者至少允许您使用调试器单步执行您的代码,并找到实际情况与您的预期不同的地方。

编辑:另外,你的两个for循环看起来很奇怪。你不应该在每个方向上看-1,0和1的偏移量(并打折0,0),而不是0 -> nRows吗?看起来你只朝一个方向看。

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

https://stackoverflow.com/questions/12921953

复制
相关文章

相似问题

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