java - void 方法递归错误

标签 java recursion

提前感谢您的帮助,基本上我正在尝试退出 void 方法的递归,但是在 return 语句之前和之后发生了一些有趣的事情。基本上该程序是找到一条穿过迷宫的路径,所以一旦它打印 YES , return 语句应该防止 rec(int x,int y) 方法的任何进一步递归,但它在打印 YES 后仍然打印,所以这就是我的问题。所以除了打印 YES 和 NO 之外,基本上所有其他 println 语句使用它们进行调试,所以如果您观察到,在打印 YES 之前,println 语句将“x”打印为 4,将“y”打印为 1,但在 return 语句之后,它们的值已更改为 2 和 1,当有无需进一步的代码来操纵它们的值。

static int x,y,fx,fy;
static char g[][]={ //your maze array , # represents wall and . represents path};
static Stack<Integer>stackx=new Stack<Integer>();
static Stack<Integer>stacky=new Stack<Integer>();
// both of the stacks are used for reverting changes int he maze to original
public static void main(String args[])
{
    x=y=0;
		for(int i=0;i<g.length;i++)
		{
			for(int j=0;j<g[i].length;j++)
			{
				if(g[i][j]=='S')
				{
					x=j;
					y=i;
				}
				else if(g[i][j]=='G')
				{
					fx=j;
					fy=i;
				}
			}
		}
    rec(x,y);
    System.out.println("HEllooooooo");
}
public static void rec(int x,int y)
{
   try
   {
       System.out.println(x+" "+y+" "+check);
       if(x==fx && y==fy)
       {
           System.out.println("YES");
           check=true;
           x=y=0;
           return;
       }
       System.out.println(x+" "+y+" "+check);
       if(check==false)
       {
                         revert();// reverts maze back to original
		 change(); // slides walls in the maze
	
		for(int i=0;i<g.length;i++)
		{
			for(int j=0;j<g[0].length;j++)
			{
				System.out.print(g[i][j]);
			}
			System.out.println("");
		}
		if(!valid(x,y+1))
		{
			if(!((y+1)>(g.length-1)))
			{
				g[y+1][x]='#';
			}
		}
		else
		{
			rec(x,y+1);
		}
		if(!valid(x+1,y))
		{
			if(!((x+1)>(g[0].length-1)))
			{
				g[y][x+1]='#';
			}
		}
		else
		{
			rec(x+1,y);
		}
		if(!valid(x-1,y))
		{
			if(!((x-1)>=0))
			{
				g[y][x-1]='#';
			}
		}
		else
		{
			rec(x-1,y);
		}
		if(!valid(x,y-1))
		{
			if(!((y-1)>=0))
			{
				g[y-1][x]='#';
			}
		}
		else
		{
			rec(x,y-1);
		}
       }
    }catch(ArrayIndexOutOfBoundsException e)
     {
        System.out.println("NO");
        return;
     }
}

输出如下

4 1 false
YES      // output is correct and should end but it continues//
2 1 true //x and y values change from 4,1 to 2,1 even with no code to manipulate them
2 1 true
3 0 true
3 0 true
4 0 true
4 0 true
NO
1 1 true
1 1 true
0 1 true
0 1 true
NO
HEllooooooo

最佳答案

如果击中 fx,fy 是最终目标,那么:

boolean rec( int x, int y )

打印“YES”后:

return true;

所有递归调用rec( ..., ... ) 应替换为

if( rec( ..., ... ) ) return true;

这应该会让你摆脱递归。

最后的返回:

return false;

关于java - void 方法递归错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27890123/

相关文章:

java - 读取 .pptx 导致 java.lang.ClassNotFoundException

c++ - 如何递归到 "print list from tail to head"?

python - Python递归缩小三角形的方法

recursion - 递归检查列表中的原子

Java 和 JS : storing results as a Java variable

java - 如果不使用 ORM,那么以 OOP 方式设计应用程序还有什么意义呢?

java - 充当 UPnP MediaServer 并通过 UPnP MediaRender 播放流音频(麦克风)

JavaFX 与 MySql 登录表单 - NullPointerException

c++ - 如何将递归转换为迭代解决方案

algorithm - 计算大 O 复杂度