以this爲例:用全局值回溯遞歸差異?
bool SolveSudoku(int grid[N][N])
{
int row, col;
// If there is no unassigned location, we are done
if (!FindUnassignedLocation(grid, row, col))
return true; // success!
// consider digits 1 to 9
for (int num = 1; num <= 9; num++)
{
// if looks promising
if (isSafe(grid, row, col, num))
{
// make tentative assignment
grid[row][col] = num;
// return, if success, yay!
if (SolveSudoku(grid))
return true;
// failure, unmake & try again
grid[row][col] = UNASSIGNED;
}
}
return false; // this triggers backtracking
}
網格始終作爲參數遞歸調用傳遞,所以在每次迭代中電網的新副本。
我似乎無法概念化使用相同的邏輯與全局網格工作是否有任何區別。
失敗後,該變量設置爲「取消&再試一次」 - 這不應該在回溯中處理任何「撤銷」嗎?
如果網格是一個全球性的,爲什麼發送和額外的複製每次這個遞歸回溯會有什麼不同?
好吧,我明白了,這是我前一陣子記得的整個「數組都是指針,好處」的東西。 –