【发布时间】:2019-01-14 01:03:43
【问题描述】:
我正在尝试用递归解决这个问题。
问题是:对于正整数的二维数组,我如何返回最长路径(步数),以便最长路径的每个单元格中的值来自整数的降序序列,并且每个单元格和单元格之间的差异是给定数字(num)。
假设 n 是单元格的值,因此 (n - num) 是正数(非零)。
我不能使用任何循环(for、while、...等)
方法:
public static int longestPath(int matrix[][],int number){...
return(__overloading__);
}
例如:
int number=1;
int [][]matrix ;
matrix = new int[][]{{8, 15, 20, 33, 35},
{60, 59, 58, 32, 31},
{59, 17, 57, 56, 55},
{55, 15, 13, 58, 16}};
System.out.print(" longestPath= "+ longestPath(matrix, num));
}
如果我们搜索相差number = 1的最长路径
1-在单元格matrix[0][3]中路径长为3,该路径中的值为33->32->31以matrix[1]结尾[4]
2-在单元格 matrix[1][0] 中,路径长为 6,路径中的值 60 -> 59 -> 58 -> 57 -> 56 -> 55以矩阵[2][4]
结尾3-在单元格matrix[1][0]中路径长为2,该路径中的值为60 -> 59以matrix[2][0]结尾
所以该方法必须返回最长的路径,它的 6
如果我们搜索相差number = 2
的最长路径1-在单元格matrix[2][1]中路径长为3,该路径中的值是17->15->13以matrix[3]结尾[2]
该方法必须返回其 3 的最长路径。
我的非工作代码:
public class CC {
public static int longestPath (int arr[][] , int num){
return longestPath(arr,arr.length-1,arr[0].length-1,num,0);
}
public static int longestPath (int arr[][],int rows,int cols,int num,int max){
System.out.println("==> longestPath() arr value=" + arr[rows][cols] + " rows:"+rows + " cols:"+cols + " max:"+max);
if (cols ==0 && rows != 0 ){
cols = arr[0].length-1;
rows--;
}
if (rows ==0 && cols==0 ){
System.out.println("finish");
return 0;
}
int steps = searchPath(arr,rows,cols,num,max);
if (steps > max) max=steps;
longestPath(arr,rows,cols-1,num,max);
return max ;
}
public static int searchPath(int arr[][],int rows,int cols,int num ,int counter){
System.out.println("searchPath() arr value=" + arr[rows][cols] + " rows:"+rows + " cols:"+cols);
int left=1,right=1,up=1,down=1;
if ((cols != 0) && arr[rows][cols] - num == arr[rows-1][cols] ){ // checking up cell
counter++;
up = searchPath(arr,rows-1,cols,num,counter);
}
if ((rows != arr.length-1) && arr[rows][cols] - num == arr[rows+1][cols] ){ // checking down cell
counter++;
down = searchPath(arr,rows+1,cols,num,counter);
// return counter;
}
if ((cols != 0) && arr[rows][cols] - num == arr[rows][cols-1]){ // checking left cell
counter++;
left = searchPath(arr,rows,cols-1,num,counter);
//return counter;
}
if ((cols != arr[0].length-1) && arr[rows][cols] - num == arr[rows][cols+1] ){ //checking right cell
counter++;
right = searchPath(arr,rows,cols+1,num ,counter);
//return counter;
}
if ((left > right) && (left > up) && (left > down)) // if left cell is bigger than all other direction return left
return left;
if ((right > left) && (right > up) && (right > down))
return right;
if ((down > up) && (down > right) &&( down > left))
return down;
if ((up> down) && (up > right) && (up>left))
return up;
return 0;
}
}
在编写代码时,我遇到了很多运行问题
我做错了什么? 提前致谢
【问题讨论】:
-
我可以看到
33 -> 32 -> 31是一个降序值为1的序列。我看不出60 -> 59 -> 58 -> 57 -> 56 -> 55为什么是一个降序值为2的序列,为什么是i@987654326 @ 一个降序值为 3 的序列。 -
我的意思是编号 1- 33 32 31. 2-60 59 58 57 56 。 3-60 59。数组中有 3 条路径,降序值为 1
-
“我遇到了很多运行问题” - 请说明您似乎遇到了哪些“问题”(错误、不正确的结果值……)。
标签: java recursion matrix path-finding longest-path