给出正整数的2D数组。 “路径”是相邻单元格的集合。两个单元格仅从右/左/上/下相邻(无对角线)。
任务是编写一个函数,该函数接收2D数组mat
,整数sum
和2D数组path
(大小与mat
-空数组相同)全零)。
该函数应该检查路径是否存在,其中单元格的总和等于sum
,如果存在则返回true,否则返回false。
数组path
将标记路径(如果存在1
)。
例如,如果mat
是:
和sum=4
然后path
可以是以下三个之一:
我的代码:
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();
}
}
仅当路径以(0,0)开始但不适用于其他路径时,我的代码才能正常工作,例如,对于
sum=16
,即使存在该路径,它也不起作用(路径数组全为零)是这样的路径。
注意:
答案 0 :(得分:1)
好问题...这是答案。这是一个有趣的代码挑战;)
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();
}
}
顺便说一下,检查多维数组维度时,您的意思是做arr.length and arr[0].length
,而您正在做arr[0].length and arr[1].length
,它将两次获得相同的维度,如果数组不是正方形,则会导致错误。
我还允许通过使用预定路径来阻止任何方向的移动,以防止再次检查同一节点。这可能是布尔数组。抱歉,如果我的x / y递归看起来很粗糙...我真的更喜欢使用循环或.forEach或=>或.map来实现...
可以使用可选参数清除-1,0,而不在第一遍进行迭代。
让我知道您是否会发现任何错误或极端情况。