0
所以,總之我正在研究一個騎士的旅程。如果你不知道那是什麼,那麼騎士會被放在棋盤上,你必須將它移動到棋盤上的每一處。我正在使用遞歸函數,但無法讓我的回溯工作。我可以在5x5板上達到22個步驟,但該程序不會備份並嘗試不同的路徑。我發佈的只是我的代碼的遞歸部分(對不起有點長)任何見解都會非常有幫助。非常感謝!C++遞歸回溯騎士遊
`bool findPath (int board[][boardSize + 4], int &currRow, int &currCol, int &currMove,int boardSize)
{
int i, j;
bool foundSpot;
board[currRow][currCol] = currMove;
if (currMove == boardSize * boardSize)
return true;
for (i = 0; i < boardSize + 4; i++)
{
for (j = 0; j < boardSize + 4; j++)
cout << setw (3) << board[i][j];
cout<<endl;
}
cout << endl;
if (board[currRow - 2][currCol - 1] == 0)
{
currMove += 1;
board[currRow - 2][currCol - 1] = currMove;
currRow -= 2;
currCol -= 1;
if (findPath(board, currRow, currCol, currMove, boardSize))
return true;
}
if (board[currRow - 2][currCol + 1] == 0)
{
currMove += 1;
board[currRow - 2][currCol + 1] = currMove ;
currRow -= 2;
currCol += 1;
if (findPath(board, currRow, currCol, currMove, boardSize))
return true;
}
if (board[currRow - 1][currCol + 2] == 0)
{
currMove += 1;
board[currRow - 1][currCol + 2] = currMove ;
currRow -= 1;
currCol += 2;
if (findPath(board, currRow, currCol, currMove, boardSize))
return true;
}
if (board[currRow + 1][currCol + 2] == 0)
{
currMove += 1;
board[currRow + 1][currCol + 2] = currMove ;
currRow += 1;
currCol += 2;
if (findPath(board, currRow, currCol, currMove, boardSize))
return true;
}
if (board[currRow + 2][currCol + 1] == 0)
{
currMove += 1;
board[currRow + 2][currCol + 1] = currMove ;
currRow += 2;
currCol += 1;
if (findPath(board, currRow, currCol, currMove, boardSize))
return true;
}
if (board[currRow + 2][currCol - 1] == 0)
{
currMove += 1;
board[currRow + 2][currCol - 1] = currMove ;
currRow += 2;
currCol -= 1;
if (findPath(board, currRow, currCol, currMove, boardSize))
return true;
}
if (board[currRow + 1][currCol - 2] == 0)
{
currMove += 1;
board[currRow + 1][currCol - 2] = currMove ;
currRow += 1;
currCol -= 2;
if (findPath(board, currRow, currCol, currMove, boardSize))
return true;
}
if (board[currRow - 1][currCol - 2] == 0)
{
currMove += 1;
board[currRow - 1][currCol - 2] = currMove ;
currRow -= 1;
currCol -= 2;
if (findPath(board, currRow, currCol, currMove, boardSize))
return true;
}
board[currRow][currCol] = 0;
currMove -= 2;
return false;
}`
歡迎來到Stack Overflow!要求人們發現代碼中的錯誤並不是特別有效。您應該使用調試器(或者添加打印語句)來分析問題,追蹤程序的進度,並將其與預期發生的情況進行比較。只要兩者發生分歧,那麼你就發現了你的問題。 (然後如果有必要,你應該構造一個[最小測試用例](http://sscce.org)。) – 2013-04-09 00:12:35
我不認爲在每個遞歸塊中改變currRow和currCol是一個好主意 - 它意味着下一個遞歸塊將從錯誤的方塊開始,並且你的'board [currRow] [currCol] = 0;'將清除最後一個遞歸,而不是你在這個級別的步驟(設置在最上面)。你爲什麼要設置開發板並在每個'if'中更改行和列變量?該遞歸電話不應該照顧那個嗎?我不認爲你希望你的行,列,移動輸入是引用 - 而不是傳值! – Rup 2013-04-09 00:16:32
而且你還需要範圍檢查每個'if(board [] [] == 0)'測試以確保兩個座標都在界限內。爲了避免重複自己,您可能需要構建一個移動排列的數組並循環遍歷它們。 – Rup 2013-04-09 00:21:48