递归时的StackOverFlowError

时间:2015-12-08 16:15:29

标签: java recursion netbeans stack-overflow

我正在使用递归算法编写游戏,但编译时遇到“StackOverFlow错误”。我知道这可能是由于没有一个好的结束条款而且它会永远重新诅咒。我只想尝试如何解决它。 我也知道我要发布的代码有点太多但我想提供尽可能多的信息。有任何疑问,请问。

它从以下调用开始:

<cfpdfform action="populate" source="test.pdf" destination="GeneratedPDFs/test.pdf" overwrite="yes">

<cfpdfformparam name="FirstLine1" value="#Variables.FirstLine#">    
<cfpdfformparam name="SecondLine1" value="#Variables.SecondLine#">    
<cfpdfformparam name="AddressBox1" value="#Variables.AddressBox#">    
<cfpdfformparam name="Body1" value="#Variables.Body1#">

<cfpdfformparam name="FirstLine2" value="#Variables.FirstLine#">    
<cfpdfformparam name="SecondLine2" value="#Variables.SecondLine#">    
<cfpdfformparam name="AddressBox2" value="#Variables.AddressBox#">    
<cfpdfformparam name="Body2" value="#Variables.Body2#">

</cfpdfform>

<cfpdf action="write" source="GeneratedPostCards/!PostCard2013-Vipre.pdf" destination="GeneratedPostCards/!PostCard2013-Vipre-flat.pdf" flatten="yes" overwrite="true">

</cfpdf>

“solver2”方法和辅助方法如下:

Integer matrix[][] = new Integer[5][5];
Deque<String> path = new ArrayDeque<>();
ArrayList<Deque<String>> paths = new ArrayList<>();
CoordenateList cList = new CoordenateList();

solver2(matrix, 0, 0, 1, path, paths, cList);

其他类(Coordenate,CoordenateList)如下:

public static void solver2(Integer[][] matrix, int x, int y, int num,
        Deque<String> path, ArrayList<Deque<String>> paths, CoordenateList cList) {

    if (cList.getCoordenateByKey(Integer.toString(y) + "," + Integer.toString(x)) == null) {
        Coordenate c = new Coordenate((Integer.toString(y) + "," + Integer.toString(x))); //substitir nos IF's
        cList.addCoordenate(c);
    }

    if (y - 3 < 0 || (cList.getCoordenateByKey(Integer.toString(y) + "," + Integer.toString(x)).getVisited())[0] == true || matrix[y - 3][x] != null) {
        //NORTE
        if (x + 3 > matrix.length - 1 || (cList.getCoordenateByKey(Integer.toString(y) + "," + Integer.toString(x)).getVisited())[1] == true || matrix[y][x + 3] != null) {
            //ESTE
            if (y + 3 > matrix.length - 1 || (cList.getCoordenateByKey(Integer.toString(y) + "," + Integer.toString(x)).getVisited())[2] == true || matrix[y + 3][x] != null) {
                //SUL
                if (x - 3 < 0 || (cList.getCoordenateByKey(Integer.toString(y) + "," + Integer.toString(x)).getVisited())[3] == true || matrix[y][x - 3] != null) {
                    //OESTE
                    if (y - 2 < 0 || x + 2 > matrix.length - 1 || (cList.getCoordenateByKey(Integer.toString(y) + "," + Integer.toString(x)).getVisited())[4] == true || matrix[y - 2][x + 2] != null) {
                        //NE
                        if (y + 2 > matrix.length - 1 || x + 2 > matrix.length - 1 || (cList.getCoordenateByKey(Integer.toString(y) + "," + Integer.toString(x)).getVisited())[5] == true || matrix[y + 2][x + 2] != null) {
                            //SE
                            if (y + 2 > matrix.length - 1 || x - 2 < 0 || (cList.getCoordenateByKey(Integer.toString(y) + "," + Integer.toString(x)).getVisited())[6] == true || matrix[y + 2][x - 2] != null) {
                                //SO
                                if (y - 2 < 0 || x - 2 < 0 || (cList.getCoordenateByKey(Integer.toString(y) + "," + Integer.toString(x)).getVisited())[7] == true || matrix[y - 2][x - 2] != null) {
                                    //NO

                                    if (num == matrix.length * matrix.length) {
                                        Deque<String> aux = new ArrayDeque<>();
                                        aux.addAll(path);
                                        paths.add(aux);
                                        path.pop();
                                        cleanRecord(num, matrix, cList); //desnecessário
                                        num--;

                                    } else {
                                        //LOSER
                                        path.pop();
                                        num--;
                                        y = getCoordenatePrevious(num, matrix)[1];
                                        x = getCoordenatePrevious(num, matrix)[0];
                                        num++;
                                        cleanRecord(num, matrix, cList);
                                        num--;
                                    }

                                } else {
                                    matrix[y - 2][x - 2] = num + 1;
                                    (cList.getCoordenateByKey(Integer.toString(y) + "," + Integer.toString(x)).getVisited())[7] = true;
                                    path.push(Integer.toString(y + 2) + "," + Integer.toString(x - 2));
                                    solver2(matrix, x - 2, y + 2, num + 1, path, paths, cList);
                                }
                            } else {
                                matrix[y + 2][x - 2] = num + 1;
                                (cList.getCoordenateByKey(Integer.toString(y) + "," + Integer.toString(x)).getVisited())[6] = true;
                                path.push(Integer.toString(y + 2) + "," + Integer.toString(x - 2));
                                solver2(matrix, x - 2, y + 2, num + 1, path, paths, cList);
                            }
                        } else {
                            matrix[y + 2][x + 2] = num + 1;
                            (cList.getCoordenateByKey(Integer.toString(y) + "," + Integer.toString(x)).getVisited())[5] = true;
                            path.push(Integer.toString(y + 2) + "," + Integer.toString(x + 2));
                            solver2(matrix, x + 2, y + 2, num + 1, path, paths, cList);
                        }
                    } else {
                        matrix[y - 2][x + 2] = num + 1;
                        (cList.getCoordenateByKey(Integer.toString(y) + "," + Integer.toString(x)).getVisited())[4] = true;
                        path.push(Integer.toString(y - 2) + "," + Integer.toString(x + 2));
                        solver2(matrix, x + 2, y - 2, num + 1, path, paths, cList);
                    }
                } else {
                    matrix[y][x - 3] = num + 1;
                    (cList.getCoordenateByKey(Integer.toString(y) + "," + Integer.toString(x)).getVisited())[3] = true;
                    path.push(Integer.toString(y) + "," + Integer.toString(x - 3));
                    solver2(matrix, x - 3, y, num + 1, path, paths, cList);
                }
            } else {
                matrix[y + 3][x] = num + 1;
                (cList.getCoordenateByKey(Integer.toString(y) + "," + Integer.toString(x)).getVisited())[2] = true;
                path.push(Integer.toString(y + 3) + "," + Integer.toString(x));
                solver2(matrix, x, y + 3, num + 1, path, paths, cList);
            }
        } else {
            matrix[y][x + 3] = num + 1;
            (cList.getCoordenateByKey(Integer.toString(y) + "," + Integer.toString(x)).getVisited())[1] = true;
            path.push(Integer.toString(y) + "," + Integer.toString(x + 3));
            solver2(matrix, x + 3, y, num + 1, path, paths, cList);
        }
    } else {
        matrix[y - 3][x] = num + 1;
        (cList.getCoordenateByKey(Integer.toString(y) + "," + Integer.toString(x)).getVisited())[0] = true;
        path.push(Integer.toString(y - 3) + "," + Integer.toString(x));
        solver2(matrix, x, y - 3, num + 1, path, paths, cList);
    }

    int cont = 0;
    if (checkIfNumExists(num, matrix)) {
        boolean[] b = cList.getCoordenateByKey(Integer.toString(getCoordenatePrevious(num, matrix)[1]) + "," + Integer.toString(getCoordenatePrevious(num, matrix)[0])).getVisited();

        if (y - 3 >= 0 && matrix[y - 3][x] == null && b[0] == false) {
            //NORTE
            cont++;
            solver2(matrix, x, y, num, path, paths, cList);
        } else if (x + 3 < matrix.length && matrix[y][x + 3] == null && b[1] == false) {
            //ESTE
            cont++;
            solver2(matrix, x, y, num, path, paths, cList);
        } else if (y + 3 < matrix.length && matrix[y + 3][x] == null && b[2] == false) {
            //SUL
            cont++;
            solver2(matrix, x, y, num, path, paths, cList);
        } else if (x - 3 >= 0 && matrix[y][x - 3] == null && b[0] == false && b[3] == false) {
            //OESTE
            cont++;
            solver2(matrix, x, y, num, path, paths, cList);
        } else if (y - 2 >= 0 && x + 2 < matrix.length && matrix[y - 2][x + 2] == null && b[4] == false) {
            //NE
            cont++;
            solver2(matrix, x, y, num, path, paths, cList);
        } else if (y + 2 < matrix.length && x + 2 < matrix.length && matrix[y + 2][x + 2] == null && b[5] == false) {
            //SE
            cont++;
            solver2(matrix, x, y, num, path, paths, cList);
        } else if (y + 2 < matrix.length && x - 2 >= 0 && matrix[y + 2][x - 2] == null && b[6] == false) {
            //SO
            cont++;
            solver2(matrix, x, y, num, path, paths, cList);
        } else if (y - 2 >= 0 && x - 2 >= 0 && matrix[y - 2][x - 2] == null && b[7] == false) {
            //NO
            cont++;
            solver2(matrix, x, y, num, path, paths, cList);
        }

    }

    if (cont == 0 && checkIfNumExists(num, matrix)) {
        if (num != 1) {
            num--;
            path.pop();
            cleanRecord(num + 1, matrix, cList);
            cleanMatrixAbove(num, matrix, cList);

        }

    }
}

/**
 * returns the coordenates of the num
 *
 * @param num
 * @param matrix
 * @return
 */
public static int[] getCoordenatePrevious(int num, Integer[][] matrix) {

    for (int i = 0; i < matrix.length; i++) {
        for (int j = 0; j < matrix.length; j++) {
            if (matrix[i][j] != null && matrix[i][j] == num) {
                int[] array = new int[2];
                array[0] = j; //x
                array[1] = i; //y
                return array;
            }
        }
    }

    return null;
}

/**
 * cleans the visited record of the num
 *
 * @param num
 * @param matrix
 * @param cList
 */
public static void cleanRecord(int num, Integer[][] matrix, CoordenateList cList) {

    if (checkIfNumExists(num, matrix)) {
        Coordenate c = cList.getCoordenateByKey(Integer.toString(getCoordenatePrevious(num, matrix)[1]) + "," + Integer.toString(getCoordenatePrevious(num, matrix)[0]));
        c.cleanVisited();
    }
}

/**
 * sets null every number in the matrix that are bigger than num
 *
 * @param num
 * @param matrix
 * @param cList
 */
public static void cleanMatrixAbove(int num, Integer[][] matrix, CoordenateList cList) {

    for (int i = 0; i < matrix.length; i++) {
        for (int j = 0; j < matrix.length; j++) {
            if (matrix[i][j] != null && matrix[i][j] > num) {
                matrix[i][j] = null;
            }
        }
    }
}

/**
 * checks if the num exists in the matrix
 * 
 * @param num
 * @param matrix
 * @return 
 */
public static boolean checkIfNumExists(int num, Integer[][] matrix) {
    for (int i = 0; i < matrix.length; i++) {
        for (int j = 0; j < matrix.length; j++) {
            if (matrix[i][j] != null && matrix[i][j] == num) {
                return true;
            }
        }
    }
    return false;
}

再一次,我很抱歉这些巨大的代码,但我真的想尽可能多地提供信息。如有任何问题或疑问,请随时提出。

1 个答案:

答案 0 :(得分:0)

如果在编译代码时遇到StackOverFlow错误,那么您可能会在编译器中发现错误。但是,在编译代码时,绝对 收到StackOverFlow错误,当您 正在运行 代码时,您会收到错误

(递归是一个高级的计算机科学主题,但是如果你仍在努力学习编译程序和运行它时的概念,那么现在递归对你来说可能有点太高级了。只是一个想法。)

在你的solver2()函数中,你有一个可怕的嵌套if()语句,除了某些不太可能的情况外,它总会递归。

然后,你有另一个if()-else语句序列,除非所有测试条件都不成立,否则它们也会一直递归。

我认为没有人会花费所有时间来阅读并理解你所拥有的这种怪异,所以唯一可以建议的是在你的递归函数中添加更多条件,这将使它成为< em>更多可能会在某些时候避免递归,这样你的递归函数就不会永远地继续递归。 (也就是说,直到它耗尽堆栈。)