Project euler 18题解答

 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
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

时间: 2024-10-23 18:52:34

Project euler 18题解答的相关文章

使用CoffeeScript来解决Project Euler中的编程问题

本 系列 文章将探讨 CoffeeScript,这是构建于 http://www.aliyun.com/zixun/aggregation/33906.html">JavaScript 基础之上的一种全新编程语言,它提供了非常干净的语法.CoffeeScript 可编译为高效的 JavaScript.除了在 Web 浏览器中运行 JavaScript 之外,您还可以将它与服务器应用程序的 Node.js 等技术一起使用.在 第 1 部分 中,学习了如何设置 CoffeeScript 编译器

用python解决project euler中的题目

寒假期间学习了python,现在基本上就能上手使用它来解决project euler里面的题目了,用python真的是没得说的,一个字"赞".在C++中需要用一大堆代码实现的算法,在python中,只需要那么短短几行.而且还有惊艳的运行速度.借用<可爱的python>里面的一句话:"人生苦短,我用python". [project euler 055] 求经过一系列规则不能得到回文数的数的个数.题目在此: If we take 47, reverse a

【Project Euler】1 第一题

 //If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23. //Find the sum of all the multiples of 3 or 5 below 1000.  static void Main()         {             int sum=0;     

【Project Euler】7 第七题

 //By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13. //What is the 10 001st prime number? static void Main(string[] args)         {             int count = 0;             for (int i = 3; i < 100000

【Project Euler】2 第二题

 //Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be: //1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ... //By considering the terms in the Fibonacci sequence whose valu

【Project Euler】5 第五题

 //2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder. //What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?         static void Main(string[] args

【Project Euler】3 第三题

 //The prime factors of 13195 are 5, 7, 13 and 29.         //What is the largest prime factor of the number 600851475143 ?         static void Main(string[] args)         {             //int[] number = new int[100];             long[] number = new

【Project Euler】9 第九题

 A Pythagorean triplet is a set of three natural numbers, a < b < c, for which, a2 + b2 = c2 For example, 32 + 42 = 9 + 16 = 25 = 52. There exists exactly one Pythagorean triplet for which a + b + c = 1000. Find the product abc. static void Main(s

【Project Euler】8 第八题

 A Pythagorean triplet is a set of three natural numbers, a < b < c, for which, a2 + b2 = c2 For example, 32 + 42 = 9 + 16 = 25 = 52. There exists exactly one Pythagorean triplet for which a + b + c = 1000. Find the product abc. static void Main(s