无法弄清楚为什么递归永远无法解决

发布于 2025-01-07 01:36:47 字数 1110 浏览 0 评论 0原文

我的朋友正在制作一个扫雷克隆版,他要求我帮助完成以下部分:当您单击非地雷/非数字“空白”方块时,它会显示所有相邻的空白。以下是我写的代码。我不明白为什么它永远不会解决。

我的基本情况应该是当 for 循环完全执行并且 if 语句永远不会返回 true 时。

我有什么遗漏的吗?

顺便说一下,这是用java编写的。另外,我告诉他所有按钮状态更改应该分配给一个方法:p

public void revealAdjacentNulls(int r, int c)
{
    int ir, ic;

    //literal edge cases :P

    int rmax = (r == 15) ? r : r + 1;
    int cmax = (c == 15) ? c : c + 1;

    //check all spaces around button at r,c

    for(ir = (r==0) ? 0 : r-1; ir <= rmax; ir++){

        for (ic = (c==0) ? 0 : c-1; ic <= cmax; ic++){

            //if any are blank and uncovered, reveal them, then check again around the blanks

            if (buttons[ir][ic].value == 0 && buttons[ir][ic].isCovered == false)
            {
                buttons[ir][ic].setEnabled(false);  //number uncovered
                buttons[ir][ic].setBackground(Color.blue);
                buttons[ir][ic].setText(Character.toString(buttons[ir][ic].value));
                buttons[ir][ic].isCovered = false;
                revealAdjacentNulls(ir, ic);
            }
        }
    }

}

My friend is making a minesweeper clone and he asked me to help with the part where when you click on a non-mine/non-number 'blank' square it reveals all adjacent blanks. The following is the code I wrote. I can't figure out why it never resolves.

My base case should be when the for loops completely execute and the if statement never returns true.

Is there something I'm missing?

This is in java, by the way. Also, I told him the whole slew of button state changing should be assigned to a method :p

public void revealAdjacentNulls(int r, int c)
{
    int ir, ic;

    //literal edge cases :P

    int rmax = (r == 15) ? r : r + 1;
    int cmax = (c == 15) ? c : c + 1;

    //check all spaces around button at r,c

    for(ir = (r==0) ? 0 : r-1; ir <= rmax; ir++){

        for (ic = (c==0) ? 0 : c-1; ic <= cmax; ic++){

            //if any are blank and uncovered, reveal them, then check again around the blanks

            if (buttons[ir][ic].value == 0 && buttons[ir][ic].isCovered == false)
            {
                buttons[ir][ic].setEnabled(false);  //number uncovered
                buttons[ir][ic].setBackground(Color.blue);
                buttons[ir][ic].setText(Character.toString(buttons[ir][ic].value));
                buttons[ir][ic].isCovered = false;
                revealAdjacentNulls(ir, ic);
            }
        }
    }

}

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

猥琐帝 2025-01-14 01:36:47

让我们考虑一下 r==0c==0 的情况,并假设 buttons[0][0].value == 0 code> 和 buttons[0][0].isCovered == false

循环的第一次迭代将导致函数使用相同的参数 0, 0 调用自身,并且 valueisCovered 的状态保持不变代码>.这将立即导致无限递归。

PS 查看维基百科文章了解其他洪水填充算法。

Let's consider the case when r==0 and c==0, and let's assume that buttons[0][0].value == 0 and that buttons[0][0].isCovered == false.

The very first iteration of the loop will cause the function to call itself with the same arguments, 0, 0, and with unchanged state of value and isCovered. This will instantly lead to infinite recursion.

P.S. Check out the Wikipedia article for other flood fill algorithms.

野却迷人 2025-01-14 01:36:47

一方面,它总是会不断递归 revealAdjacentNulls(r, c)。您的条件是 isCovered 必须为 false - 但随后您也将 isCovered 设置为 false。您的意思是写:

buttons[ir][ic].isCovered = true;

?或者您的支票可能应该是:(

if (buttons[ir][ic].value == 0 && buttons[ir][ic].isCovered)

这取决于您所说的“承保”的含义。)

Well for one thing, it will always keep recursing for revealAdjacentNulls(r, c). Your condition is that isCovered must be false - but then you're setting isCovered to false as well. Did you mean to write:

buttons[ir][ic].isCovered = true;

? Or possibly your check should be:

if (buttons[ir][ic].value == 0 && buttons[ir][ic].isCovered)

(It depends on what you mean by "is covered".)

顾北清歌寒 2025-01-14 01:36:47

另一种情况:如果 r == 15,则循环将从 14 (r - 1) 到 15 (rmax)。如果你的 if 语句为真,那么就会有无限递归。这同样适用于c。

Another case: if r == 15 then the loop will be from 14 (r - 1) to 15 (rmax). If your if statement is true, then there will be infinite recursion. The same applies to c.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文