Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/389.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 - Fatal编程技术网

Java 给定二维整数数组,查找递归求和到给定数字的路径

Java 给定二维整数数组,查找递归求和到给定数字的路径,java,recursion,Java,Recursion,给定一个2d数组,我需要递归地返回一个矩阵,其路径和给定的数字相加 路径矩阵应为零,但总和为给定数字的路径除外,该路径将标记为1 路径只能在路径中左/右上/下移动 我尝试过所有的可能性,首先检查我是否已经去过下一个街区 问题是,经过一些迭代之后,它停止了,不再返回并再次清除块为0 private static boolean paintPath(int[][] mat, int sum, int[][] path , int i, int j){ if(sum<0)

给定一个2d数组,我需要递归地返回一个矩阵,其路径和给定的数字相加

路径矩阵应为零,但总和为给定数字的路径除外,该路径将标记为1

路径只能在路径中左/右上/下移动

我尝试过所有的可能性,首先检查我是否已经去过下一个街区

问题是,经过一些迭代之后,它停止了,不再返回并再次清除块为0

private static boolean paintPath(int[][] mat, int sum, int[][] path , int i, int j){

        if(sum<0)
            return false;

        path[i][j] = 1;

        if(sum == 0)
            return true;
        printMat(path);
        System.out.println();

        if(withinBoundaries(mat,i+1,j) && path[i+1][j] != 1)
            if(paintPath(mat,sum - mat[i][j],path,i+1,j))
                return true;

        if(withinBoundaries(mat,i,j+1) && path[i][j+1] != 1)
            if(paintPath(mat,sum - mat[i][j],path,i,j+1))
                return true;

        if(withinBoundaries(mat,i-1,j) && path[i-1][j] != 1)
            if(paintPath(mat,sum - mat[i][j],path,i-1,j))
                return true;
        if(withinBoundaries(mat,i,j-1) && path[i][j-1] != 1)
            if(paintPath(mat,sum - mat[i][j],path,i,j-1))
                 return true;

        path[i][j] = 0;
        return false;

    }
随叫随到

paintPath(mat1, 5, path, 0, 0);
我希望算法会返回

        {0,0,0,0},
        {1,0,0,0},
        {0,0,0,0},
        {0,0,0,0}
我明白了

相反

从对paintPath的调用(mat1400,path,0,0); 我想

        {0,0,0,0},
        {0,1,1,0},
        {0,1,1,0},
        {0,0,0,0}

问题是我的算法从(0,0)开始,然后从那里寻找路径,我需要它从任何起点(没有循环)找到任何路径

编辑: 作业中引用的问题: “我们将数组中的路径定义为相邻单元格的集合。 相邻的CEKK可以从左、右、上、下相邻,但不能从对角线相邻。 每个单元只能在路径中累积一次。 编写一个递归方法,它接受一个2d数组mat,该数组mat包含大于0的整数、一个整数和泊松数,以及一个与mat大小相同的2d数组路径

该方法需要检查数组中是否存在其值之和等于sum的路径。 如果有这样一个路径,它应该返回true,false或otherwize

数组路径用于标记其和等于和的路径

路径单元格中的位置为1,其他单元格中的位置为0

该方法必须是完全没有循环的递归! 因此,正如您将编写的任何其他方法一样

可以使用重载

您不能更改垫子。

更改此垫子

path[i][j] = 1;

if(sum == 0)
  return true;
printMat(path);
System.out.println();
为此:

if(sum == 0){
  printMat(path);
  System.out.println();
  return true;
}

path[i][j] = 1;
注意,您将获得输出

{0,0,0,0},
{1,0,0,0},
{0,0,0,0},
{0,0,0,0}
如果您将起始呼叫更改为

paintPath(mat1, 5, path, 1, 0);

你能详细解释一下,从何处到何处的路径必须有什么总和吗?@user8426627(我似乎无法正确格式化它,将它粘贴到文本编辑器中阅读)我猜从任何起点到任何其他点的路径总和为给定的数。例如给定数字10和以下矩阵-`{1,2,3,1},{5,8,7,15},{1,2,3,4},{1,1,1,1}`他想得到下列任何一个-`01 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0对于n~矩阵维,它是有实际任务还是只是有用的?输出是一个路径矩阵,我添加了一些示例,希望它能澄清我写了一些解释,我需要它从相同的起始位置(0,0)而不是(1,0)找到路径。我添加了更多的示例,以便任务更容易理解。谢谢!这只是一个旁注,这个答案确实解决了您的问题。在将路径标志设置为1之前,您需要检查
sum==0
。@OmerSegal我答案的第一部分还解决了代码中导致返回错误解决方案的错误。因此修正,在第一次调用中,您当前的函数适用于任何给定的起始点。您现在提出的问题已经改变——请澄清要求。我们可以解决“不使用循环,“通过将此工作函数与另一个递归函数一起使用,该函数只需尝试每个起点。为了理解原因,需要更多的细节。如果问题是提供给你的,比如作业或网站上列出的,你可以引用全部细节。@•㪞עדברקן我添加了作业中的问题,希望它澄清递归修复它,谢谢!
if(sum == 0){
  printMat(path);
  System.out.println();
  return true;
}

path[i][j] = 1;
{0,0,0,0},
{1,0,0,0},
{0,0,0,0},
{0,0,0,0}
paintPath(mat1, 5, path, 1, 0);