我正在尝试用蛮力方法解决博客问题。输入是:
表示为字符串的网格矩阵,例如:'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)
答案 0 :(得分:2)
您正在混合有符号和无符号算术。 matsize
为int
,grid_index
为unsigned int
。您与>0
的比较无效,因为结果总是无符号的,因此从不消极。
顺便说一下,修复签名/未签名的不匹配后,您可能需要>=0
。
答案 1 :(得分:1)
如果您尝试在条件列表前显示cout << grid_index-1-matsize << endl;
。您将看到由于grid_index
未签名而发生缓冲区下溢。
出于这个原因,条件(grid_index-matsize)<grid.length()
为真,因为grid_index
已超过grid.length()
。
您必须将函数checkqueue
的签名更改为
bool Boogle::checkqueue(const string &grid, const string &word, bool* const &isvisited, int grid_index, unsigned int count) const