0

I made it so that if a losing candidate in any pair wins in another, a function checks whether there's the losing candidate in that pair wins in any other, and so forth. this here is the unlocks the pair if the function returns a true value (indicating that it'd cause a cycle)

for (int i = 0; i < pair_count; i++)
{
    for (int j = 0; j < pair_count; j++)
    {
        if (locked[pairs[j].loser][i] && check(i, pairs[j].loser) == true)
        {
            locked[pairs[j].loser][i] = false;
        }
    }
}

And here is the function that checks for cycles

bool check(int j, int y)
{
    if (locked[j][y] == true)
    {
        return true;
    }

    for (int i = 0; i < pair_count; i++)
    {
        if (locked[j][i] && check(i, y) == true)
        {
        return true;
        }
    } 

return false;}

So basically, if a loser in one pair, wins in another, this function checks if that loser wins in another, recursively, until it reaches a point where the loser doesn't win, the for loop ends, and it returns false, otherwise, if it keeps going, and reaches the original winner (the second input in check), it returns true.

It successfully unlocks final pairs if they create cycles, and in the manual tests i've tried, it worked for middle pairs as well, but check50 says that it doesn't work, are there any cases that my function fails to account for?

i initially thought that if the loser won against more than one person, the function would check only the first one, and if it's false then it would mess things up, but i was wrong since after all the checks, it'd go back to the first (or last, i'm not sure how that's called in recursion) for loop and it'd check again for the second.

i'm stumped and i can't figure out where my program goes wrong, i'd really appreciate your help. Thanks in advance

1

I found it unnecessary to check multiple branches to the same node the way I executed my lock cycles function. I was very surprised by this. Whether this is related to the fact the graph does not have multiple sources for the purposes of this assignment, possibly.

| improve this answer | |

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .