Java 查找“;路径“;在2D数组中,路径单元格之和等于;sum";
给定正整数的二维数组。“路径”是相邻单元的集合。两个单元格仅从右/左/上/下(无对角线)相邻 任务是编写一个函数,用于接收二维数组Java 查找“;路径“;在2D数组中,路径单元格之和等于;sum";,java,recursion,backtracking,Java,Recursion,Backtracking,给定正整数的二维数组。“路径”是相邻单元的集合。两个单元格仅从右/左/上/下(无对角线)相邻 任务是编写一个函数,用于接收二维数组mat、整数sum和二维数组path(与mat大小相同-空数组均为零) 该函数应检查单元格和等于sum的路径是否存在,如果存在则返回true,否则返回false 数组path将标记路径(如果存在1) 例如,如果mat为: 和sum=4 然后,路径可以是以下三种路径之一: 我的代码: public static void main(String[] args) {
mat
、整数sum
和二维数组path
(与mat
大小相同-空数组均为零)
该函数应检查单元格和等于sum
的路径是否存在,如果存在则返回true,否则返回false
数组path
将标记路径(如果存在1
)
例如,如果mat
为:
和sum=4
然后,路径
可以是以下三种路径之一:
我的代码:
public static void main(String[] args)
{
int[][] mat={{2,41,3,14},
{2,1,24,7},
{2,15,10,54},
{63,22,2,4}};
int[][] path={{0,0,0,0},
{0,0,0,0},
{0,0,0,0},
{0,0,0,0}};
findSum(mat,4,path);
//print(mat);
print(path);
}
public static boolean findSum (int mat[][], int sum, int path[][])
{
return findSum(mat,sum,path,0,0);
}
public static boolean findSum (int mat[][], int sum, int path[][],int i,int j)
{
if(sum==0)
return true;
if(i==mat[0].length||j==mat[1].length)
return false;
boolean result=findSum(mat,sum-mat[i][j],path,i,j+1)||findSum(mat,sum-mat[i][j],path,i+1,j);
if(result)
path[i][j]=1;
return result;
}
private static void print(int[][] arr)
{
for(int i=0;i<arr[0].length;i++)
{
for(int j=0;j<arr[0].length;j++)
{
System.out.print(arr[i][j]+" ");
}
System.out.println();
}
}
publicstaticvoidmain(字符串[]args)
{
int[]mat={{2,41,3,14},
{2,1,24,7},
{2,15,10,54},
{63,22,2,4}};
int[]path={{0,0,0,0},
{0,0,0,0},
{0,0,0,0},
{0,0,0,0}};
findSum(mat,4,path);
//印刷品(mat);
打印(路径);
}
公共静态布尔findSum(int-mat[],int-sum,int-path[]
{
返回findSum(mat,sum,path,0,0);
}
公共静态布尔findSum(int mat[]]、int sum、int path[]]、int i、int j)
{
如果(总和=0)
返回true;
如果(i==mat[0]。长度| | j==mat[1]。长度)
返回false;
布尔结果=findSum(mat,sum mat[i][j],路径,i,j+1)| | findSum(mat,sum mat[i][j],路径,i+1,j);
如果(结果)
路径[i][j]=1;
返回结果;
}
专用静态无效打印(int[]arr)
{
对于(inti=0;i这个问题很好…下面是答案。这是一个有趣的代码挑战;)
publicstaticvoidmain(字符串[]args)
{
int[]mat={{2,41,3,14},
{2,1,24,7},
{2,15,10,54},
{63,22,2,4}};
int[]path={{0,0,0,0},
{0,0,0,0},
{0,0,0,0},
{0,0,0,0}};
if(findSum(mat,22,path))打印(path);
else System.out.println(“未找到路径”);
}
公共静态布尔findSum(int-mat[],int-sum,int-path[]
{
返回起始路径(mat,sum,path,-1,0);
}
//递归地检查每个可能的起点
公共静态布尔起始路径(int mat[]],int sum,int path[]],int y,int x)
{
//迭代y,必要时转到下一列
如果(++y==材料长度){
y=0;
++x;
}
if(x==mat[0].length)//边界检查
返回false;
if(findSum(mat,sum,path,y,x))//我们找到了一个成功的起点!
{
System.out.println(“成功路径从“+x+”、“+y”开始);
返回true;
}
返回起始路径(mat,sum,path,y,x);//我们必须继续查找
}
公共静态布尔findSum(int mat[]]、int sum、int path[]]、int i、int j)
{
如果(i==mat[0].length | | j==mat[1].length | | i是查找可能路径或查看给定路径和求和是否对给定矩阵有效的任务?任务是编写一个递归函数,该函数接收一个mat
(正整数的二维数组)、sum
和path
(零的二维数组)。如果有一个路径,其和等于给定的sum
变量,则函数应重新返回true
。为什么我们要在path中设置值?sum必须重置,并且您必须在每个单元格上再次执行整个检查。此外,如果路径的形状类似于字母,则仅向右和向下移动的方法将不起作用J.您只能找到从左上角单元格开始的路径,因为它是您开始搜索的唯一位置,而这恰恰发生在findSum(mat,sum,path,0,0);
中。
public static void main(String[] args)
{
int[][] mat={{2,41,3,14},
{2,1,24,7},
{2,15,10,54},
{63,22,2,4}};
int[][] path={{0,0,0,0},
{0,0,0,0},
{0,0,0,0},
{0,0,0,0}};
if ( findSum(mat,22,path) ) print(path);
else System.out.println("No path found");
}
public static boolean findSum (int mat[][], int sum, int path[][])
{
return startPath(mat, sum, path, -1, 0);
}
// Recursively check every possible starting point
public static boolean startPath(int mat[][], int sum, int path[][], int y, int x)
{
// Iterate y, goto next column if necessary
if (++y == mat.length) {
y = 0;
++x;
}
if (x == mat[0].length) // Bounds check
return false;
if (findSum(mat, sum, path, y, x)) // We've found a successful start point!
{
System.out.println("A successful path starts at " + x + ", " + y);
return true;
}
return startPath(mat, sum, path, y, x); // We'll have to keep looking
}
public static boolean findSum (int mat[][], int sum, int path[][], int i, int j)
{
if(i==mat[0].length || j==mat[1].length || i<0 || j<0) // Bounds check
return false;
if (path[i][j] == 1) // Backtracking check
return false;
sum -= mat[i][j]; // Decrement sum
if (sum >= 0) { // More to go? look around
path[i][j] = 1;
if (sum == 0) return true; // We made it!
// If any path finds the end, don't try other paths
boolean result = findSum(mat, sum, path, i+1, j);
if (result) return true;
result = findSum(mat, sum, path, i, j+1);
if (result) return true;
result = findSum(mat, sum, path, i-1, j);
if (result) return true;
result = findSum(mat, sum, path, i, j-1);
// There was no successful paths, this is a dead end
if (!result) path[i][j] = 0;
return result;
} else { // We're negative, overshot it
return false;
}
}
private static void print(int[][] arr)
{
for(int i=0;i<arr[0].length;i++)
{
for(int j=0;j<arr[0].length;j++)
{
System.out.print(arr[i][j]+" ");
}
System.out.println();
}
}