简体   繁体   中英

CS50- pset3-tideman: lock_pairs did not correctly lock all non-cyclical pairs

Not able to figure out the test case, where this code is failing. All green except for this error: CS50- pset3-tideman: lock_pairs did not correctly lock all non-cyclical pairs. I need to know the logical flaw here, more than the solution.

// Lock pairs into the candidate graph in order, without creating cycles

void lock_pairs(void)
{
    // TODO
    for (int i = 0; i < pair_count; i ++)
    {
        if(!is_circle(pairs[i].loser, pairs[i].winner))
        {
            locked[pairs[i].winner][pairs[i].loser] = true;
        }

    }
    return;
}

bool is_circle(int loser, int winner)
{
    if (loser == winner)
    {
        return true;
    }
    for (int i = 0; i < pair_count; i ++)
    {
        if (locked[loser][i])
        {
            return is_circle(i, winner);
        }
    }
    return false;
}

If the recursive call of is_circle returns false , the function may not return immediately, but has to check possible other pair edges from the local i :

            return is_circle(i, winner);

has to be

            if (is_circle(i, winner)) return true;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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