CS50' tideman:lock_pairs如果创建一个周期(据称),则无法跳过最后一对

发布于 2025-01-24 17:05:47 字数 2637 浏览 2 评论 0 原文

我陷入了tideman的锁定功能。我已经为此编写了代码,看起来不错。它在通过调试器运行时也可以表现出应有的行为;但是,每次我尝试使用Check50尝试一下,就会在“如果创建一个周期的情况下跳过最后一对”上的错误。 为了尝试一下,我使用了示例,其中最终对应该跳过一对,并且当我逐步观看调试器的整个过程时,一切都按计划进行,所以我真的不知道我什么我在这里缺少。

这是问题的解释:

void lock_pairs(void)
{
    // TODO
    // When no_cycle is 0, it indicates that there is no cycle and that locking the pair is safe
    int no_cycle = 0;

    for (int i = 0; i < pair_count; i++)
    {
        for (int j = 0; j < pair_count; j++)
        {
            // We check each pair, looking for a locked pair where the loser of the pair we're checking is the winner.
            if (!locked[pairs[i].loser][pairs[j].loser])
            {
                no_cycle = 0;
                continue;
            }
            else
            {
                // If the result is true, we check for a possible cycle using the cycle_check function
                if (cycle_check(i, j) == false)
                {
                    // If the result is false, we lock the pair and move on
                    no_cycle = 0;
                    break;
                }
                else
                {
                    // If the result is true (indicating a cycle), we change the value of no_cycle to avoid locking the pair
                    no_cycle = 1;
                    break;
                }
            }
        }
        if (no_cycle == 0)
        {
            locked[pairs[i].winner][pairs[i].loser] = true;
        }
        else continue;
    }
    return;
}

bool cycle_check(i, j)
{
    // We check if the loser of the locked pair is the same as our pair's winner or starting point (the end of the chain is also the begining)
    if (pairs[j].loser == pairs[i].winner)
    {
        return true;
    }
    else
    {
        //If it isn't, we go through each locked pair, following the chain which could lead to the cycle
        for (int h = 0; h < pair_count; h++)
        {
            if (locked[pairs[j].loser][pairs[h].loser])
            {
                // Once we find the next link in the chain, we start over
                j = h;
                if (cycle_check(i, j) == true)
                {
                    // Once we find that there is indeed a cycle, we break recursion
                    return true;
                }
            }
        }
        // If we follow the chain but don't find a cycle, the function returns false and the pair is locked
        return false;
    }
}

永远陷入困境。任何帮助都将不胜感激。

顺便说一句,英语不是我的主要语言,所以请原谅任何语法错误。

I'm stuck at the lock_pairs function of Tideman. I already wrote the code for it and it looks good. It also behaves as it should when running it through the debugger; however, every time I try it with check50 it gives me an error on "lock pair skips final pair if it creates a cycle".
In order to try it out, I've used examples where the final pair is the one which should be skipped and everything goes as planned when I watch the whole process step by step on the debugger, so I really don't know what I'm missing here.

Here's the explanation of the problem: https://cs50.harvard.edu/x/2022/psets/3/tideman/

void lock_pairs(void)
{
    // TODO
    // When no_cycle is 0, it indicates that there is no cycle and that locking the pair is safe
    int no_cycle = 0;

    for (int i = 0; i < pair_count; i++)
    {
        for (int j = 0; j < pair_count; j++)
        {
            // We check each pair, looking for a locked pair where the loser of the pair we're checking is the winner.
            if (!locked[pairs[i].loser][pairs[j].loser])
            {
                no_cycle = 0;
                continue;
            }
            else
            {
                // If the result is true, we check for a possible cycle using the cycle_check function
                if (cycle_check(i, j) == false)
                {
                    // If the result is false, we lock the pair and move on
                    no_cycle = 0;
                    break;
                }
                else
                {
                    // If the result is true (indicating a cycle), we change the value of no_cycle to avoid locking the pair
                    no_cycle = 1;
                    break;
                }
            }
        }
        if (no_cycle == 0)
        {
            locked[pairs[i].winner][pairs[i].loser] = true;
        }
        else continue;
    }
    return;
}

bool cycle_check(i, j)
{
    // We check if the loser of the locked pair is the same as our pair's winner or starting point (the end of the chain is also the begining)
    if (pairs[j].loser == pairs[i].winner)
    {
        return true;
    }
    else
    {
        //If it isn't, we go through each locked pair, following the chain which could lead to the cycle
        for (int h = 0; h < pair_count; h++)
        {
            if (locked[pairs[j].loser][pairs[h].loser])
            {
                // Once we find the next link in the chain, we start over
                j = h;
                if (cycle_check(i, j) == true)
                {
                    // Once we find that there is indeed a cycle, we break recursion
                    return true;
                }
            }
        }
        // If we follow the chain but don't find a cycle, the function returns false and the pair is locked
        return false;
    }
}

Been stuck forever on this. Any help would be much appreciated.

Btw, english is not my primary language, so please excuse any grammar errors.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文