Java堆栈中的递归方法

时间:2020-04-01 18:31:18

标签: 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, t, arr, i + 1));
    }
    return false;
}

arr中的数组都填充了(填充的)int [2]。 (尝试遍历数组以查看kt是否确实存在)。 TIA

1 个答案:

答案 0 :(得分:0)

//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;

     }