无法弄清楚为什么递归永远无法解决
我的朋友正在制作一个扫雷克隆版,他要求我帮助完成以下部分:当您单击非地雷/非数字“空白”方块时,它会显示所有相邻的空白。以下是我写的代码。我不明白为什么它永远不会解决。
我的基本情况应该是当 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
让我们考虑一下
r==0
和c==0
的情况,并假设buttons[0][0].value == 0
code> 和buttons[0][0].isCovered == false
。循环的第一次迭代将导致函数使用相同的参数
0, 0
调用自身,并且value
和isCovered
的状态保持不变代码>.这将立即导致无限递归。PS 查看维基百科文章了解其他洪水填充算法。
Let's consider the case when
r==0
andc==0
, and let's assume thatbuttons[0][0].value == 0
and thatbuttons[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 ofvalue
andisCovered
. This will instantly lead to infinite recursion.P.S. Check out the Wikipedia article for other flood fill algorithms.
一方面,它总是会不断递归
revealAdjacentNulls(r, c)
。您的条件是isCovered
必须为 false - 但随后您也将isCovered
设置为 false。您的意思是写:?或者您的支票可能应该是:(
这取决于您所说的“承保”的含义。)
Well for one thing, it will always keep recursing for
revealAdjacentNulls(r, c)
. Your condition is thatisCovered
must be false - but then you're settingisCovered
to false as well. Did you mean to write:? Or possibly your check should be:
(It depends on what you mean by "is covered".)
另一种情况:如果 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.