By starting at the top of the triangle below and moving to adjacent numbers on the row below, the maximum total from top to bottom is 23.
3
7 4
2 4 6
8 5 9 3
That is, 3 + 7 + 4 + 9 = 23.
Find the maximum total from top to bottom of the triangle below:
75
95 64
17 47 82
18 35 87 10
20 04 82 47 65
19 01 23 75 03 34
88 02 77 73 07 63 67
99 65 04 28 06 16 70 92
41 41 26 56 83 40 80 70 33
41 48 72 33 47 32 37 16 94 29
53 71 44 65 25 43 91 52 97 51 14
70 11 33 28 77 73 17 78 39 68 17 57
91 71 52 38 17 14 91 43 58 50 27 29 48
63 66 04 68 89 53 67 30 73 16 69 87 40 31
04 62 98 27 23 09 70 98 73 93 38 53 60 04 23
NOTE: As there are only 16384 routes, it is possible to solve this problem by trying every route. However, Problem 67, is the same challenge with a triangle containing one-hundred rows; it cannot be solved by brute force, and requires a clever method! ;o)
最简单的方法就是穷举,从根节点出发,每个节点都有两个分叉,到达底部的路径有估计有2的指数级的数目(有会算的朋友请留言,我的组合数学都还给老师了),不过这道题显然是符合动态规划的特征,往下递增一层的某个节点的最佳结果f[i][j]肯定是上一层两个入口节点对应的最佳结果的最大值,也就是f[i-1][j]或者f[i-1][j+1],递归的边界就是定点f[0][0]=75。因此我的解答如下,考虑了金字塔边界的情况,数据按照金字塔型存储在numbers.txt中,
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Euler18Problem {
public static void maxSun(int[][] a, int rows, int cols) {
// 结果列表
int[][] f = new int[15][15];
// 路径,用于输出计算路径
int[][] path = new int[15][15];
// 递归边界
f[0][0] = a[0][0];
path[0][0] = 0;
// 递推
for (int i = 1; i < rows; i++) {
int col = i + 1;
// 决策
for (int j = 0; j < col; j++) {
// 左边界
if (j - 1 < 0) {
f[i][j] = f[i - 1][j] + a[i][j];
path[i][j] = j;
} else if (j + 1 > col) { // 右边界
f[i][j] = f[i - 1][j - 1] + a[i][j];
path[i][j] = j - 1;
} else {
// 处于中间位置
if (f[i - 1][j] <= f[i - 1][j - 1]) {
f[i][j] = f[i - 1][j - 1] + a[i][j];
path[i][j] = j - 1;
} else {
f[i][j] = f[i - 1][j] + a[i][j];
path[i][j] = j;
}
}
}
}
// 求出结果
int result = 0, col = 0;
for (int i = 0; i < cols; i++) {
if (f[14][i] > result) {
result = f[14][i];
col = i;
}
}
// 输出路径
System.out.println("row=14,col=" + col + ",value=" + a[14][col]);
for (int i = rows - 2; i >= 0; i--) {
col = path[i][col];
System.out.println("row=" + i + ",col=" + col + ",value="
+ a[i][col]);
}
System.out.println(result);
}
public static void main(String[] args) throws Exception {
int rows = 15;
int cols = 15;
int[][] a = new int[rows][cols];
BufferedReader reader = new BufferedReader(new InputStreamReader(
Euler18Problem.class.getResourceAsStream("/numbers.txt")));
String line = null;
int row = 0;
while ((line = reader.readLine()) != null && !line.trim().equals("")) {
String[] numbers = line.split(" ");
for (int i = 0; i < numbers.length; i++) {
a[row][i] = Integer.parseInt(numbers[i]);
}
row++;
}
reader.close();
maxSun(a, rows, cols);
}
}
执行结果如下,包括了路径输出:
row=14,col=9,value=93
row=13,col=8,value=73
row=12,col=7,value=43
row=11,col=6,value=17
row=10,col=5,value=43
row=9,col=4,value=47
row=8,col=3,value=56
row=7,col=3,value=28
row=6,col=3,value=73
row=5,col=2,value=23
row=4,col=2,value=82
row=3,col=2,value=87
row=2,col=1,value=47
row=1,col=0,value=95
row=0,col=0,value=75
1074
ps.并非我闲的蛋疼在半夜做题,只是被我儿子折腾的无法睡觉了,崩溃。
文章转自庄周梦蝶 ,原文发布时间2009-09-27