You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

CS50 Tideman lock_pairs函数循环检测异常:check50报错未跳过成环配对

问题:lock_pairs函数无法通过check50检测,提示未正确锁定所有非循环配对

我实现了一个递归函数,用于检测配对中的败者是否曾获胜:若循环至初始胜者则判定出现环,并跳过该胜者。同时将被跳过的胜者设为负数,以便算法复用。其中pairs是存储胜者和败者的数组,locked[i][j]为布尔数组,当i击败j时值为true。

手动创建测试用pairs数组时,即使之前已形成环,最后一个配对仍能正确设为True,但check50仍报错:

lock_pairs did not correctly lock all non-cyclical pairs

附上代码:

typedef struct
{
    int winner;
    int loser;
}
pair;

// Lock pairs into the candidate graph in order, without creating cycles
void lock_pairs(void)
{
    for (int i = 0; i < pair_count; i++)
    {
        global_status = false; 
        check(i, i);

            if (global_status == true)
            {
                locked[pairs[i].winner][pairs[i].loser] = false;
            }
            else
            {
                locked[pairs[i].winner][pairs[i].loser] = true;
            }
    }
    return;
}

void check(int current_winner, int i)   
{
    if (global_status == false)
    {
        for (int j = 0; j <= i; j++)
        {
            if (pairs[current_winner].loser == pairs[j].winner)
            {
                if (j == i) // if j = i then the current winner in locked_pairs creates a cycle
                {
                    global_status = true;
                    pairs[i].winner = -1; // where i is the initial winner in lock_pairs
                }
                return check(j, i); 
            }
        }
    }
}

内容的提问来源于stack exchange,提问作者Jack Kirby

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.07 15:40:29