Project Euler:Problem 82 Path sum: three ways

The minimal path sum in the 5 by 5 matrix below, by starting in any cell in the left column and finishing in any cell in the right column, and only moving up, down, and right, is
indicated in red and bold; the sum is equal to 994.

????????131201630537805673968036997322343427464975241039654221213718150111956331????????

Find the minimal path sum, in matrix.txt (right
click and "Save Link/Target As..."), a 31K text file containing a 80 by 80 matrix, from the left column to the right column.

动态规划

从第一列任意位置开始,只能向上向下向右移动到达最后一列,使得经过数字的和最小。因为不知道是从哪个起点出发的元素路径和最小,把这所有的路径和存在一个一维数组ans[80]中。该数组的初始值为矩阵第一列上的元素。

第一列上的元素接下来的移动一定是向右的,不可能向上或者向下,因为第一列上的任意元素都可以作为起始元素。

动态规划体现在在元素向右和向下比较和向右和向上比较中。

在元素向右和向下比较中

ans[j-1]是比较新的值,是已经加到第i列上的元素了的,而ans[j]是比较旧的值,才算到第i-1列上的元素。

ans[j]的新值应该是ans[j-1]+ls[j][i]和ans[j]+ls[j][i]中最小的那个。

在元素向右向上比较中:

这些ans[j]都是算到第i的元素上的了

ans[j+1]要想移动到ans[j]要算上ls[j][i]才行

所以ans[j]的新值是ans[j]和ans[j+1]+ls[j][i]的最小值。

这个矩阵下标是左上角为(79,79)右下角为(0,0)

最后输出ans中的最小值即可。

ls=[]
for line in open("matrix.txt"):
    #print(line)
    a=line.split(',')
    a=[int(i) for i in a]
    ls.append(a)

ans=[ls[i][79] for i in range(80)]

for i in range(78,-1,-1):
    ans[0]=ans[0]+ls[0][i]

#向下
    for j in range(1,80):
        ans[j]=min(ans[j]+ls[j][i],ans[j-1]+ls[j][i])

#向上
    for j in range(78,-1,-1):
        ans[j]=min(ans[j],ans[j+1]+ls[j][i])

print(min(ans))

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-11-05 06:27:00

Project Euler:Problem 82 Path sum: three ways的相关文章

Project Euler:Problem 13 Large sum

Work out the first ten digits of the sum of the following one-hundred 50-digit numbers. 37107287533902102798797998220837590246510135740250 46376937677490009712648124896970078050417018260538 74324986199524741059474233309513058123726617309629 919422133

Project Euler:Problem 46 Goldbach's other conjecture

It was proposed by Christian Goldbach that every odd composite number can be written as the sum of a prime and twice a square. 9 = 7 + 2×12 15 = 7 + 2×22 21 = 3 + 2×32 25 = 7 + 2×32 27 = 19 + 2×22 33 = 31 + 2×12 It turns out that the conjecture was f

Project Euler:Problem 40 Champernowne's constant

An irrational decimal fraction is created by concatenating the positive integers: 0.123456789101112131415161718192021... It can be seen that the 12th digit of the fractional part is 1. If dn represents the nth digit of the fractional part, find the v

Project Euler:Problem 18 Maximum path sum I

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

Project Euler:Problem 67 Maximum path sum II

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 in triangle.txt (righ

Project Euler:Problem 56 Powerful digit sum

A googol (10100) is a massive number: one followed by one-hundred zeros; 100100 is almost unimaginably large: one followed by two-hundred zeros. Despite their size, the sum of the digits in each number is only 1. Considering natural numbers of the fo

Project Euler:Problem 50 Consecutive prime sum

The prime 41, can be written as the sum of six consecutive primes: 41 = 2 + 3 + 5 + 7 + 11 + 13 This is the longest sum of consecutive primes that adds to a prime below one-hundred. The longest sum of consecutive primes below one-thousand that adds t

Project Euler:Problem 16 Power digit sum

215 = 32768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 = 26. What is the sum of the digits of the number 21000? #include <iostream> #include <string> using namespace std; int main() { string s = "1"; for (int i = 1; i <= 1000;

Project Euler:Problem 20 Factorial digit sum

n! means n × (n ? 1) × ... × 3 × 2 × 1 For example, 10! = 10 × 9 × ... × 3 × 2 × 1 = 3628800, and the sum of the digits in the number 10! is 3 + 6 + 2 + 8 + 8 + 0 + 0 = 27. Find the sum of the digits in the number 100! #include <iostream> #include &