java - 调试器跳过递归 Java 程序中的方法

标签 java recursion

我正在尝试编写一个代码,打印在二维数组中的单元格之间跳转所需的最大步骤,仅当用户给出的 int Num 是旁边的单元格。

我尝试调试,但行 return Q3(A , NUM ,0, 0,0) 跳过命令

public class Ex14 {
    public static int longestSlope(int[][] A, int num) {

        return recursion(0, 0, A, num);
    }


    private static int recursion(int row, int col, int[][] A, int NUM) {
        if (col < A[0].length)
            return recursion(row, col + 1, A, NUM);
        if (row < A.length)
            recursion(row + 1, col, A, NUM);

        return Q3(A, NUM, 0, 0, 0);
    }


    private static int Q3(int[][] A, int NUM1, int ind1, int ind2, int Step) {

        if ((ind1 + 1) < A.length && A[(ind1 + 1)][(ind2 + 0)] == A[ind1][ind2] - NUM1)
            return Q3(A, NUM1, ind1 + 1, ind2, Step + 1);
        if ((ind1 - 1) > A.length && A[(ind1 - 1)][(ind2 + 0)] == A[ind1][ind2] - NUM1)
            return Q3(A, NUM1, ind1 - 1, ind2, Step + 1);
        if ((ind2 + 1) < A[0].length && A[(ind1 - 0)][(ind2 + 1)] == A[ind1][ind2] - NUM1)
            return Q3(A, NUM1, ind1, ind2 + 1, Step + 1);
        if ((ind2 - 1) > A[0].length && A[(ind1 + 0)][(ind2 - 1)] == A[ind1][ind2] - NUM1)
            return Q3(A, NUM1, ind1, ind2 - 1, Step + 1);
        else return Step; //if a cell next to current index is in the same slop move to that cell and add 1 step

    }
}

这是一个测试器:

public class Tester14 {

    public static void main() {

        /* Tester Question 3 */

        System.out.println("********** Question 3 **********\n");

        int[][] mat = {
            {
                3,
                13,
                15,
                28,
                30
            },
            {
                55,
                54,
                53,
                27,
                26
            },
            {
                54,
                12,
                52,
                51,
                50
            },
            {
                50,
                10,
                8,
                53,
                11
            }
        };

        int num = 1;
        System.out.println("Test1: num => 1");
        System.out.println("Expected result => 6, Student result = " +
            Ex14.longestSlope(mat, num) + "\n");


    }
}

最佳答案

我发现了两个问题:

  • Q3 递归仅从顶行的单元格开始, 最右边的列。它应该从每个细胞开始。因此,只需使用 2 个 for 循环来循环所有单元格,而不是使用递归(当然,这也可以做到)。
  • 2 个 if 语句是错误的。请参阅下面 2 个 if 语句中的 >= 0

这两个问题已解决:

public static int longestSlope(int[][] A, int num) {
    int max = 0;
    for (int row = 0; row < A.length; row++) {
        for (int col = 0; col < A[0].length; col++) {
            max = Math.max(max, Q3(A, num, row, col, 1));
        }
    }
    return max;
}

private static int Q3(int[][] A, int NUM1, int ind1, int ind2, int Step) {
    if ((ind1 + 1) < A.length && A[(ind1 + 1)][(ind2 + 0)] == A[ind1][ind2] - NUM1)
        return Q3(A, NUM1, ind1 + 1, ind2, Step + 1);
    if ((ind1 - 1) >= 0 && A[(ind1 - 1)][(ind2 + 0)] == A[ind1][ind2] - NUM1)
        return Q3(A, NUM1, ind1 - 1, ind2, Step + 1);
    if ((ind2 + 1) < A[0].length && A[(ind1 - 0)][(ind2 + 1)] == A[ind1][ind2] - NUM1)
        return Q3(A, NUM1, ind1, ind2 + 1, Step + 1);
    if ((ind2 - 1) >= 0 && A[(ind1 + 0)][(ind2 - 1)] == A[ind1][ind2] - NUM1)
        return Q3(A, NUM1, ind1, ind2 - 1, Step + 1);
    else return Step; //if a cell next to current index is in the same slop move to that cell and add 1 step
}

关于java - 调试器跳过递归 Java 程序中的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54008346/

相关文章:

java - 在运行时动态添加实体类

Java JFrame setSize 无法正常工作

c - C编程中的索引

Java 递归博弈论取得最佳进展

java - 深复制多维数组

java - Spring 配置创建两个 bean 而不是一个

能不能把每一个递归都改成迭代呢?

python - 检查元素集是否可以共同创建父元素

javascript - 如何在 javascript 数据结构上使用递归在 Angular 2 中查找和添加新数据

java - AddFirst() 方法在我的链接列表中不起作用