我一直在尝试解决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;
}
会解决它
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句