這是我第一次使用遞歸做一些事情,而不是找到一個數的階乘。我正在製作一個程序,以便在一塊麻煩的板子裏找到單詞。以下是導致段錯誤的功能:C++遞歸段錯誤。你能幫我看看我做錯了什麼嗎?
void findWord(vector<string>& board, set<string>& dictionary,
string prefix, int row, int column){
prefix += getTile(board, row, column);
if(prefix.length() > biggestWordLength)
return;
if(isOutOfBounds(row, column))
return;
if(isWord(prefix, dictionary) == 1)
foundWords.insert(prefix);
if(isWord(prefix, dictionary) == 0)
return;
//Note: this does not prevent using the same tile twice in a word
findWord(board, dictionary, prefix, row-1, column-1);
findWord(board, dictionary, prefix, row-1, column);
findWord(board, dictionary, prefix, row-1, column+1);
findWord(board, dictionary, prefix, row, column-1);
findWord(board, dictionary, prefix, row, column+1);
findWord(board, dictionary, prefix, row+1, column-1);
findWord(board, dictionary, prefix, row+1, column);
findWord(board, dictionary, prefix, row+1, column+1);
}
你應該把邊界檢查放在你添加一個字符到前綴末尾的部分 – Brian