0

ブルートフォースアプローチでブーグルの問題を解決しようとしています。入力は次のとおりです。

文字列として表されるグリッド マトリックス。たとえば、:'pesa' 文字列 'asa' としても表される単語。

単語がマトリックス内の正当な単語であるかどうかを確認する関数を作成しています。

bool Boogle::contains(std::string grid, std::string word) const
{
    bool* isvisited=new bool[grid.length()];
    for (unsigned int i=0; i<grid.length(); i++)
    {
        *(isvisited+i)=false;
    }

    for (unsigned int i=0; i<grid.length(); i++)
    {
        // Recursive approach
        if (grid[i]==word[0])
            if (checkqueue(grid, word, isvisited, i, 0))
                return true;    
    }
    return false;
}

bool Boogle::checkqueue(const string &grid, const string &word, bool* const &isvisited, unsigned int grid_index, unsigned int count) const
{   
    int matsize=int(sqrt(grid.length()));
    cout<<"\nCurrently at the index "<<grid_index<<"\n";
    isvisited[grid_index]=true;
    for (unsigned int i=0; i<grid.length(); i++)
    {
        cout <<isvisited[i]<<" ";
    }
    cout<<"\n";

    if (count==word.length()-1)
    {
        cout << " reach the end of word\n";
        return true;
    }
    else
    {
        count ++;
        cout << "Recursive call on WORD: "<<word<<"  " <<count<<" "<<word[count]<<"\n";

        // non diagonal
        if ((grid_index<grid.length()) && (isvisited[grid_index+1]==false) && (grid[grid_index+1]==word[count]))
            return checkqueue(grid, word, isvisited, grid_index+1, count);

        else if ((grid_index>0)&& (isvisited[grid_index-1]==false)  && (grid[grid_index-1]==word[count]))
            return checkqueue(grid, word, isvisited, grid_index-1, count);

        else if (((grid_index+matsize)<grid.length())&& (isvisited[grid_index+matsize]==false)  && (grid[grid_index+matsize]==word[count]))
            return checkqueue(grid, word, isvisited, grid_index+1, count);

        else if (((grid_index-matsize)<grid.length())&& (isvisited[grid_index-matsize]==false)  && (grid[grid_index-matsize]==word[count]))
            return checkqueue(grid, word, isvisited, grid_index+1, count);

        // diagonal
        else if ((grid_index-1-matsize>0)&& (isvisited[grid_index-1-matsize]==false)  && (grid[grid_index-1-matsize]==word[count]))
            return checkqueue(grid, word, isvisited, grid_index-1-matsize, count);

        else if ((grid_index+1-matsize>0) && (isvisited[grid_index+1-matsize]==false) && (grid[grid_index+1-matsize]==word[count]))
            return checkqueue(grid, word, isvisited, grid_index+1-matsize, count);

        else if ((grid_index+1+matsize<grid.length())&& (isvisited[grid_index+1+matsize]==false)  && (grid[grid_index+1+matsize]==word[count]))
            return checkqueue(grid, word, isvisited, grid_index+1+matsize, count);

        else if ((grid_index-1+matsize<grid.length())&& (isvisited[grid_index-1+matsize]==false)  && (grid[grid_index-1+matsize]==word[count]))
            return checkqueue(grid, word, isvisited, grid_index-1+matsize, count);
        else
        {
            // cout<<"No possible neighbor\n";
            return false;
        }

    }
}

boogle.contains("pesa","as") を実行するとうまくいきます。しかし、それが「asa」などの不正な単語である場合は、セグメンテーション フォールトが返されます。それはどこから来たのですか?

./Boogle.exe 
.
Currently at the index 3
0 0 0 1 
Recursive call on WORD: asa  1 s

Currently at the index 2
0 0 1 1 
Recursive call on WORD: asa  2 a
Segmentation fault: 11

P/S: 単語が有効な場合、これは正しい実行です (boogle.contains("pesa","esp"))

Currently at the index 1
0 1 0 0 
Recursive call on WORD: esp  1 s

Currently at the index 2
0 1 1 0 
Recursive call on WORD: esp  2 p

Currently at the index 3
0 1 1 1 
 reach the end of word



OK (1 tests)
4

2 に答える 2