Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-cloud-platform/3.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递归在2D数组中查找峰值_Java_Recursion_2d - Fatal编程技术网

如何停止Java递归在2D数组中查找峰值

如何停止Java递归在2D数组中查找峰值,java,recursion,2d,Java,Recursion,2d,所以我一直在努力完善这个方法,但我无法让它发挥作用。我试图做的是在Java中使用递归在不同整数的2D数组中找到一个峰值数 基本上我的方法是什么?它检查索引是否在数组中,以及上面、下面、左边和右边的数字是否比当前数字大 如果是,递归将打印当前点并继续。但是,对于我编写的代码,该方法找到第一条路径,然后出于某种原因返回并找到另一条路径,这会产生问题,我只希望打印一条路径,然后该方法需要停止 我试着用一个布尔值来检查是否有峰值,然后返回true,但它仍然返回并打印其他路径。如果你们能帮我,那就太棒了

所以我一直在努力完善这个方法,但我无法让它发挥作用。我试图做的是在Java中使用递归在不同整数的2D数组中找到一个峰值数

基本上我的方法是什么?它检查索引是否在数组中,以及上面、下面、左边和右边的数字是否比当前数字大

如果是,递归将打印当前点并继续。但是,对于我编写的代码,该方法找到第一条路径,然后出于某种原因返回并找到另一条路径,这会产生问题,我只希望打印一条路径,然后该方法需要停止

我试着用一个布尔值来检查是否有峰值,然后返回true,但它仍然返回并打印其他路径。如果你们能帮我,那就太棒了

代码如下:

private static void printPath (int[][] mat, int i, int j) {

    System.out.println("("+i+","+j+")");

    if (i >=0 && i < mat.length-1 && mat[i][j] < mat[i+1][j]){
        printPath(mat,i+1,j);
    }
    if (j >=0 && j < mat[0].length-1 && mat[i][j] < mat[i][j+1]){
        printPath(mat,i,j+1);
    }
    if (i>0 && i < mat.length-1 && mat[i][j] < mat[i-1][j]){
        printPath(mat,i-1,j);
    }
    if (j>0 && j < mat[0].length-1 && mat[i][j] < mat[i][j-1]){
        printPath(mat,i,j-1);
    }


}    
private static void打印路径(int[]mat,int i,int j){
System.out.println(“(“+i+”,“+j+”);
如果(i>=0&&i=0&&j0&&i0&&j
为什么不改变整个算法

  • 通过依次添加
    m
    数组中的
    n
    项,将矩阵展平为
    m*n
    大小的一维数组

  • 使用simple
    max
    算法查找展平阵列中峰值的索引

  • 将展平阵列中的索引转换为原始矩阵中的点:

    i = index / m
    j = index % m
    
  • 编辑

    如果出现以下情况,请尝试将
    else
    关键字放在这些
    关键字之间:

    private static void printPath (int[][] mat, int i, int j) {
    
        System.out.println("("+i+","+j+")");
    
        if (i >=0 && i < mat.length-1 && mat[i][j] < mat[i+1][j]){
            printPath(mat,i+1,j);
        } else if (j >=0 && j < mat[0].length-1 && mat[i][j] < mat[i][j+1]){
            printPath(mat,i,j+1);
        } else if (i>0 && i < mat.length-1 && mat[i][j] < mat[i-1][j]){
            printPath(mat,i-1,j);
        } else if (j>0 && j < mat[0].length-1 && mat[i][j] < mat[i][j-1]){
            printPath(mat,i,j-1);
        }
    }   
    
    private static void打印路径(int[]mat,int i,int j){
    System.out.println(“(“+i+”,“+j+”);
    如果(i>=0&&i=0&&j0&&i0&&j

    但我仍然不确定算法——这将能够找到一个局部峰值,但不是全局峰值——想象一下,有一个项目的所有邻居都比它自己低,但在矩阵的其他地方可能有更大的数字。您的算法将停止在该项上,即使它不是所有算法中最大的一项。

    为什么不更改整个算法

  • 通过依次添加
    m
    数组中的
    n
    项,将矩阵展平为
    m*n
    大小的一维数组

  • 使用simple
    max
    算法查找展平阵列中峰值的索引

  • 将展平阵列中的索引转换为原始矩阵中的点:

    i = index / m
    j = index % m
    
  • 编辑

    如果出现以下情况,请尝试将
    else
    关键字放在这些
    关键字之间:

    private static void printPath (int[][] mat, int i, int j) {
    
        System.out.println("("+i+","+j+")");
    
        if (i >=0 && i < mat.length-1 && mat[i][j] < mat[i+1][j]){
            printPath(mat,i+1,j);
        } else if (j >=0 && j < mat[0].length-1 && mat[i][j] < mat[i][j+1]){
            printPath(mat,i,j+1);
        } else if (i>0 && i < mat.length-1 && mat[i][j] < mat[i-1][j]){
            printPath(mat,i-1,j);
        } else if (j>0 && j < mat[0].length-1 && mat[i][j] < mat[i][j-1]){
            printPath(mat,i,j-1);
        }
    }   
    
    private static void打印路径(int[]mat,int i,int j){
    System.out.println(“(“+i+”,“+j+”);
    如果(i>=0&&i=0&&j0&&i0&&j
    但我仍然不确定算法——这将能够找到一个局部峰值,但不是全局峰值——想象一下,有一个项目的所有邻居都比它自己低,但在矩阵的其他地方可能有更大的数字。您的alogrithm将停止在此项,即使它不是所有项中最大的。

    它“返回”的原因是每个递归调用可能有4个分支。让我们考虑一个例子:

  • 第一个条件为true,因此第一条路径开始
  • 在某个点上,没有满足任何条件,路径结束
  • 程序返回到最后一帧(),并从它结束的地方执行代码它“返回”的原因是每个递归调用可能有4个分支。让我们考虑一个例子:

  • 第一个条件为true,因此第一条路径开始
  • 在某个点上,没有满足任何条件,路径结束

  • 程序返回到最后一帧并从其结束处执行代码如果您要查找任何一条可能的路径,而不是具有最大值的路径,我有一个简单的解决方案


    只需将剩余的if语句作为else if语句。您强制程序在递归函数的每次调用中只遵循一条路径

    如果要查找任何一条可能的路径,而不是具有最大值的路径,我有一个简单的解决方案


    只需将剩余的if语句作为else if语句。您强制程序在递归函数的每次调用中只遵循一条路径。。。在这种情况下,我不建议您只使用else语句,因为这样您只会显示找到的第一条高路径。我已经重写了你的代码以找到最高的矩阵路径。显然,它变得更加复杂,但您可以确保找到最高路径

    private static void printPath (int[][] mat, int i, int j) {
    
        if (mat.length == 0 || mat[0].length == 0) {
            System.out.println("Empty matrix");
            return;
        }
    
        System.out.println("("+i+","+j+")");
    
        int rightValue = i >=0 && i < mat.length-1 && mat[i][j] < mat[i+1][j] ? mat[i+1][j] : mat[i][j];
        int belowValue = j >=0 && j < mat[0].length-1 && mat[i][j] < mat[i][j+1] ? mat[i][j+1] : mat[i][j];
        int aboveValue = i>0 && i < mat.length-1 && mat[i][j] < mat[i-1][j] ? mat[i-1][j] : mat[i][j];
        int leftValue = j>0 && j < mat[0].length-1 && mat[i][j] < mat[i][j-1] ? mat[i][j-1] : mat[i][j];
    
        // now you need to iterate over the four values to check wich one is the highest value
        // this way, you will get the highest path... 
    
        if (rightValue > leftValue) {
            if (rightValue > belowValue) {
                if (rightValue > aboveValue) {
                    printPath(mat,i+1,j);
                } else {
                    printPath(mat,i,j+1);
                }
            } else {
                if (belowValue > aboveValue) {
                    printPath(mat,i-1,j);
                } else {
                    printPath(mat,i,j+1);
                }
            }
        } else {
            if (leftValue > belowValue) {
                if (leftValue > aboveValue) {
                    printPath(mat,i-1,j);
                } else {
                    printPath(mat,i,j+1);
                }
            } else {
                if (belowValue > aboveValue) {
                    printPath(mat,i-1,j);
                } else {
                    printPath(mat,i,j+1);
                }
            }
        }
    }
    
    private static void打印路径(int[]mat,int i,int j){