0

だから私はJavaでBoggleプログラムに取り組んでおり、可能なすべての単語を見つけるのに苦労しています。

それらのほとんどすべてを見つけますが、私の人生では、なぜそれらすべてを取得できないのかわかりません。

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% しか見つからない理由を知っている人はいますか?

必要に応じて、さらにコードを添付できます。

4

1 に答える 1

1

いくつかのテスト ケースを作成することをお勧めします。これを行うと、問題がすぐに見つかるか、少なくともデバッガーを使用してコードをステップ実行し、現実が期待とは異なる場所を見つけることができるようになります。

編集: また、2 つの for ループは奇妙に見えます。0 -> nRows ではなく、各方向に -1,0 と 1 のオフセット (および 0,0 を割引) を見るべきではありませんか? 一方向だけを見ているようです。

于 2012-10-16T19:21:46.027 に答える