I'm new to stackoverflow and pretty new to programming. I'm working on the tideman problem for the CS50 course. https://cs50.harvard.edu/x/2020/psets/3/tideman/ When I run check50 everything checks out except for one:
:( lock_pairs skips final pair if it creates cycle lock_pairs did not correctly lock all non-cyclical pairs
These two do pass the test: :) lock_pairs locks all pairs when no cycles :) lock_pairs skips middle pair if it creates a cycle
I can't find the problem. What am I missing here?
This is my code:
// Each pair has a winner, loser
typedef struct
{
int winner;
int loser;
}
pair;
// Array of candidates
string candidates[MAX];
pair pairs[MAX * (MAX - 1) / 2];
// Lock pairs into the candidate graph in order, without creating cycles
void lock_pairs(void)
{
// for every pair we need to check for a circle
for (int i = 0; i < pair_count; i++)
{
if (!circle_check(pairs[i].winner, pairs[i].loser))
{
//there's no circle: lock in pair
locked[pairs[i].winner][pairs[i].loser] = true;
}
}
}
// check pair for circles between winner and loser. Loser is first link
bool circle_check(int winner, int link)
{
// check if the loser already has connections
for (int n = 0; n < candidate_count; n++)
{
if (locked[link][n] == true)
{
// there's a link. if this ends in the winner, there's a circle
if (n == winner)
{
return true;
}
else
{
// there may still be a circle, check next connection
link = n;
circle_check(winner, link);
}
}
}
return false;
}