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)
最簡(jiǎn)單的方法就是窮舉,從根節(jié)點(diǎn)出發(fā),每個(gè)節(jié)點(diǎn)都有兩個(gè)分叉,到達(dá)底部的路徑有估計(jì)有2的指數(shù)級(jí)的數(shù)目(有會(huì)算的朋友請(qǐng)留言,我的組合數(shù)學(xué)都還給老師了),不過(guò)這道題顯然是符合動(dòng)態(tài)規(guī)劃的特征,往下遞增一層的某個(gè)節(jié)點(diǎn)的最佳結(jié)果f[i][j]肯定是上一層兩個(gè)入口節(jié)點(diǎn)對(duì)應(yīng)的最佳結(jié)果的最大值,也就是f[i-1][j]或者f[i-1][j+1],遞歸的邊界就是定點(diǎn)f[0][0]=75。因此我的解答如下,考慮了金字塔邊界的情況,數(shù)據(jù)按照金字塔型存儲(chǔ)在numbers.txt中,
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Euler18Problem {
public static void maxSun(int[][] a, int rows, int cols) {
// 結(jié)果列表
int[][] f = new int[15][15];
// 路徑,用于輸出計(jì)算路徑
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;
}
}
}
}
// 求出結(jié)果
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);
}
}
執(zhí)行結(jié)果如下,包括了路徑輸出:
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.并非我閑的蛋疼在半夜做題,只是被我兒子折騰的無(wú)法睡覺(jué)了,崩潰。