[英]CS50's pset3 Tideman... my lock function doesn't seem to work
我一直在嘗試解決 lock_pairs 函數的問題。 我使用遞歸來驗證我們是否有一條遵循循環的路徑(基本上,遵循失敗者的路徑,看看它是否戰勝了另一個候選人,並檢查我們是否回到了原點,有效地形成了一個循環)。
但是我有一個問題,因為它在使用 check50 驗證我的解決方案時返回錯誤,而且我太迷茫了,甚至想不出哪里出了問題,我覺得我已經檢查了所有內容,但仍然無法正常工作。
這是 check50 的錯誤:
:( lock_pairs skips final pair if it creates cycle
lock_pairs did not correctly lock all non-cyclical pairs
這是代碼:
// To be used in lock_pairs to avoid cycles, true means cycles, false means we good
bool check_cycles(int loser_ind, int winner_ind)
{
// Base case, if the loser path returns to the winner, we have a circle
if (loser_ind == winner_ind)
{
return true;
}
for (int i = 0; i < candidate_count; i++)
{
if (locked[loser_ind][i]) // If the loser has an edge over another candidate, i.e, if the loser candidate won against another candidate
{
return check_cycles(i, winner_ind); // We'll check if that other candidate that lost has a winning path to another candidate
}
}
return false;
}
// Lock pairs into the candidate graph in order, without creating cycles
void lock_pairs(void)
{
// Let's loop on all pairs
for (int i = 0; i < pair_count; i++)
{
// If we didn't created a cycle, we'll register the lock
if (!check_cycles(pairs[i].loser, pairs[i].winner))
{
locked[pairs[i].winner][pairs[i].loser] = true; // It means the i-th pair winner candidate won over i-th pair losing candidate
}
}
return;
}
也許改變
return check_cycles(i, winner_ind);
到
if (check_cycles(i, winner_ind)){
return true;
}
會修復它
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.