我正在從「Java照明」一書中做遞歸章節問題。我需要在正整數的矩形網格中找到總和最大的路徑,從最上一行的任意位置開始,然後在每個後續步驟中直接向下或向下移動。查找從最上一行向下一行向下移動的最大總和路徑
到目前爲止,我已經能夠計算出最大路徑的實際總和。在下面的網格
02 05 17 12 03
15 08 04 11 10
09 18 06 20 16
14 13 12 01 07
我現有的代碼將返回60
我怎樣才能返回(或打印出)與最大和的路徑?即17 + 11 + 20 + 12 = 60
public class MaximumPath {
public static int maxSum(int grid[][]){
int r = grid.length - 1; // bottom row #
int c = grid[0].length - 1; // rightmost column #
int max = 0;
for (int i=0; i <= c; i++){
int val = maxSum(grid, r, i); // call recursive method for each of the bottom row cells
if (val > max) max = val;
}
return max;
}
public static int maxSum(int grid[][], int row, int col)
// recursive method to find largest sum path to row,col, coming downwards from top
{
if (col < 0 || row < 0 || row > grid[0].length || col > grid.length) return 0;
else return grid[row][col] + max3( maxSum(grid, row-1, col-1), // top left
maxSum(grid, row-1, col), // top
maxSum(grid, row-1, col+1)) ; // top right
}
public static int max3(int x, int y, int z) // return max of 3 numbers
{
if (x > y)
return (x > z)? x : z;
else
return (y > z)? y : z;
}
public static void main(String[] args){
int[][] grid = {{2, 5, 17, 12, 3},
{15, 8, 4, 11, 10},
{9, 18, 6, 20, 16},
{14, 13, 12, 1, 7}};
System.out.println("Max sum is "+maxSum(grid));
}
}
這是完美的 - 我試着在各個地方放置幾個System.out.println來調試遞歸如何工作,但無法弄清楚。 – matt