繁体   English   中英

CS50的pset3 Tideman...我的锁功能好像不起作用

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM