java图遍历求最长路径_如何在Java中使用递归实现矩阵中最长路径的返回

我正试图用递归来解决这个问题。

问题是:对于二维正整数数组,我如何返回最长路径(步骤),以便最长路径的每个单元格中的值是从整数的降序序列开始的,并且每个单元格和单元格之间的差异是一个给定的数字(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));

}

如果我们寻找有差异的最长路径

= 1

1-在细胞中

矩阵〔0〕〔3〕

路径长度为3,此路径中的值为33->32->31以

矩阵〔1〕〔4〕

2-细胞

矩阵〔1〕〔0〕

]路径长度为6,路径60->59->58->57->56->55中的值以

矩阵〔2〕〔4〕

细胞中的3

矩阵〔1〕〔0〕

路径长度为2,此路径中的值为60->59以

矩阵〔2〕〔0〕

因此,该方法必须返回最长路径开关6

如果我们寻找有差异的最长路径

= 2

1-在细胞中

矩阵〔2〕〔1〕

路径长度为3,此路径中的值为17->15->13以

矩阵〔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;

}

}

在编写代码时,我遇到了很多运行问题

我做错什么了?

提前谢谢

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值