Java,跟踪鼠迷的递归

时间:2016-10-22 03:25:14

标签: java recursion trace

我正在研究Rat Maze问题(如果是1,你可以向右或向下移动,如果为0,你可以向下移动)。我有问题,我的结果不会返回堆栈,我跟踪它,一切似乎都很好,然后我改变了我的代码,它工作,我不知道我的跟踪是如何不正确的。

我将发布3个版本的代码,首先纠正然后2个错误。我的问题是关于每个递归代码的正确跟踪是什么,以便我可以在下次正确地跟踪我自己的代码并理解我正在通过它正确地跟踪以找到错误。

我应该提到结果是可能达到[n-1] [m-1]

的路径数

更正代码

nodeSvg = g.selectAll(".node")
    .data(nodes,function(d) { return d.id; });

代码错误1 即使在基本情况下更新后,结果也始终为调用堆栈的0 我只是摆脱了' result ='在递归的情况下

 import java.io.*;
import java.util.*;

class Solution {  
public static int findpath(int [][] arr){
   int row = 0;
   int col = 0;
   int result = 0;
   return findpathHelper(arr, row, col, result);

}

public static int findpathHelper(int [][] arr, int row, int col, int result){

System.out.println(row);
System.out.println(col);
System.out.println();

if(col == arr[row].length-1 && row == arr.length-1){
  System.out.println("Result: " +result);
   return result+1;
}

if(row+1 != arr.length && arr[row+1][col] != 0){      
  result = findpathHelper(arr, row+1, col, result);
}

if(col+1 != arr[row].length && arr[row][col+1] != 0){     
  result = findpathHelper(arr, row,col+1, result);
}


return result;    
  }
  public static void main(String[] args) {
   int [][] path = {{1,1,1,1},
                {0,1,1,1},
                {1,0,0,1},
                {1,1,1,1}};

   System.out.println(findpath(path));         
  }
}

代码错误2 在基本情况下,我摆脱了返回,而是使用return + = 1, 在第一个调用堆栈中它是1但是当它返回堆栈时它返回到0。

import java.io.*;
import java.util.*;



class Solution {  
  public static int findpath(int [][] arr){
    int row = 0;
    int col = 0;
    int result = 0;
   return findpathHelper(arr, row, col, result);

 }

public static int findpathHelper(int [][] arr, int row, int col, int result){

System.out.println(row);
System.out.println(col);
System.out.println();

if(col == arr[row].length-1 && row == arr.length-1){
  System.out.println("Result: " +result);
   return result+1;
}

if(row+1 != arr.length && arr[row+1][col] != 0){      
  findpathHelper(arr, row+1, col, result);
}

if(col+1 != arr[row].length && arr[row][col+1] != 0){     
  findpathHelper(arr, row,col+1, result);
}
return result;    
}

  public static void main(String[] args) {
  int [][] path = {{1,1,1,1},
                {0,1,1,1},
                {1,0,0,1},
                {1,1,1,1}};

  System.out.println(findpath(path));         
 }
}

Aditional Edit 去第二个错误代码,如果我在递归情况下添加return =但不是基本情况代码仍然看起来很好 import java.io. ;     import java.util。;

import java.io.*;
import java.util.*;



class Solution {  
  public static int findpath(int [][] arr){
    int row = 0;
    int col = 0;
    int result = 0;
   return findpathHelper(arr, row, col, result);

 }

public static int findpathHelper(int [][] arr, int row, int col, int result){

System.out.println(row);
System.out.println(col);
System.out.println();

if(col == arr[row].length-1 && row == arr.length-1){
  System.out.println("Result: " +result);
   result+=1;
}

if(row+1 != arr.length && arr[row+1][col] != 0){      
  return = findpathHelper(arr, row+1, col, result);
}

if(col+1 != arr[row].length && arr[row][col+1] != 0){     
  return = findpathHelper(arr, row,col+1, result);
}
return result;    
}

  public static void main(String[] args) {
  int [][] path = {{1,1,1,1},
                {0,1,1,1},
                {1,0,0,1},
                {1,1,1,1}};

  System.out.println(findpath(path));         
 }
}

1 个答案:

答案 0 :(得分:0)

啊经典的错误。 您忘记在if条件内返回结果。

你有这个:

if(col+1 != arr[row].length && arr[row][col+1] != 0){     
  findpathHelper(arr, row,col+1, result);
}

你想要的是这个:

if(col+1 != arr[row].length && arr[row][col+1] != 0){     
  return findpathHelper(arr, row,col+1, result);
}
相关问题