Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java堆栈溢出中的递归方法_Java_Recursion_Stack Overflow - Fatal编程技术网

java堆栈溢出中的递归方法

java堆栈溢出中的递归方法,java,recursion,stack-overflow,Java,Recursion,Stack Overflow,我在以下代码中得到stackoverflow: private static boolean haveBeen(int k, int t,ArrayList <int[]> arr, int i){ if(!(i==arr.size ())) { if (arr.get (i)[0] == k && arr.get (i)[1] == t) return true; return (haveBeen (k

我在以下代码中得到stackoverflow:

private static boolean haveBeen(int k, int t,ArrayList <int[]> arr, int i){
    if(!(i==arr.size ())) {
        if (arr.get (i)[0] == k && arr.get (i)[1] == t)
            return true;
        return (haveBeen (k, t, arr, i + 1));
    }
    return false;
}
private static boolean haveBeen(int k,int t,ArrayList arr,int i){
如果(!(i==arr.size()){
if(arr.get(i)[0]==k&&arr.get(i)[1]==t)
返回true;
返回(havebes(k,t,arr,i+1));
}
返回false;
}
arr
中的数组都填充了(填充的)int[2]。(尝试迭代数组以查看
k
t
是否确实存在)。 TIA

//prince需要从初始坐标i,j,in在最短路径中捕捉恶棍
//非负整数的平方矩阵。如果
//-2-1)//向左走
if(isLegal(drm[i][j],drm[i-1][j])&&(havebed(i-1,j,visited,0)))
minPathLength=Math.min(递归增量(drm,i,j,i-1,j,visitedSquareScope)+1,minPathLength);
如果(drm[0]。长度>j+1)//向上
if(isLegal(drm[i][j],drm[i][j+1])&&(havebed(i,j+1,visited,0)))
minPathLength=Math.min(递归增量(drm,i,j,i,j+1,VisitedSquareScope)+1,minPathLength);
如果(j-1>-1)//下降
if(isLegal(drm[i][j],drm[i][j-1])&&(havebed(i,j-1,visited,0)))
minPathLength=Math.min(递归增量(drm,i,j,i,j-1,VisitedSquareScope)+1,minPathLength);
返回Math.min(drm.length*2,minPathLength);//大于最大可能路径,因此表示未找到任何路径。
}
私有静态布尔值(int k,int t,ArrayList arr,int i){
如果(!(i==arr.size()){
if(arr.get(i)[0]==k&&arr.get(i)[1]==t)
返回true;
返回(havebes(k,t,arr,i+1));
}
返回false;
}
私有静态布尔isLegal(int currotofVal,int adjcRoofVal){
返回((curroofval-adjcRoofVal=0))| |(adjcRoofVal curroofval==1)| | adjcRoofVal==1;
}

i的初始值是什么??0:()()()()()()()()()()40。一个循环就行了。另外,
if(!(i==arr.size()){
可以写得更清楚,如
if(i!=arr.size()){
。最后,必须递归…好的。你能创建一个吗?我试过你的代码,它似乎有效:Edited-villain=-1(第三行)
//prince needs to catch villain in shortest path from initial coordinates i,j, in 
//square matrix of non negative ints .Can move to adjacent squares if 
//-2<(square-adjacent square)<3. returns -1 if no path exists. villain is -1.
public static int prince(int[][] drm, int i, int j){
        ArrayList<int[]> firstSquare = new ArrayList<int[]>();
        int[] currentCoordinates = {i,j};
        firstSquare.add (currentCoordinates);
        int returnValue = recursePrince (drm,i,j,0,0,firstSquare) ;
        if (returnValue == drm.length*drm.length)
           return -1;
        return returnValue;
    }


    private static int recursePrince(int[][] drm, int i, int j, int nextSquareX, int nextSquareY, ArrayList <int[]> visitedSquares) {
        int minPathLength=drm.length*drm.length;
        int[] currentCoordinates = {i,j};
        visitedSquares.add (currentCoordinates);
        ArrayList<int[]> visitedSquaresCopy = (ArrayList<int[]>) visitedSquares.clone ();
        if (drm[nextSquareX][nextSquareY]==-1) //exit condition
            return 0;
        if (drm.length > i+1)//go right
            if (isLegal (drm[i][j], drm[i + 1][j]) && !(haveBeen (i + 1, j, visitedSquaresCopy, 0))) {
                minPathLength = Math.min (recursePrince (drm, i, j, i + 1, j, visitedSquaresCopy) + 1, minPathLength);
            }        if (i-1 > -1)//go left
            if (isLegal (drm[i][j], drm[i - 1][j]) && !(haveBeen (i -1, j, visitedSquaresCopy, 0)))
                minPathLength = Math.min (recursePrince (drm, i, j, i - 1, j, visitedSquaresCopy)+1,minPathLength);
        if (drm[0].length> j+1)//go up
            if (isLegal (drm[i][j], drm[i][j+1]) && !(haveBeen (i, j+1, visitedSquaresCopy, 0)))
                minPathLength = Math.min (recursePrince (drm, i, j, i, j+1, visitedSquaresCopy)+1,minPathLength);
        if (j-1>-1)//go down
            if (isLegal (drm[i][j], drm[i][j-1]) && !(haveBeen (i, j-1, visitedSquaresCopy, 0)))
                minPathLength = Math.min (recursePrince (drm, i, j, i , j-1, visitedSquaresCopy)+1,minPathLength);
        return Math.min(drm.length*2,minPathLength);// Larger than max possible path so will represent NO PATH FOUND.
    }
    private static boolean haveBeen(int k, int t,ArrayList <int[]> arr, int i){
        if(!(i==arr.size ())) {
            if (arr.get (i)[0] == k && arr.get (i)[1] == t)
                return true;
            return (haveBeen (k, t, arr, i + 1));
        }
        return false;
    }
     private static boolean isLegal(int currRoofVal,int adjcRoofVal){
        return ((currRoofVal - adjcRoofVal<=2) && (currRoofVal-adjcRoofVal>=0)) || (adjcRoofVal-currRoofVal==1) || adjcRoofVal==-1;

     }