EularProject 67: 三角形数字矩阵的最大路径
来源:互联网 发布:淘宝抵用券使用说明 编辑:程序博客网 时间:2024/05/16 02:22
Maximum path sum II
Problem 67
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 (right click and 'Save Link/Target As...'), a 15K text file containing a triangle with one-hundred rows.
NOTE: This is a much more difficult version of Problem 18. It is not possible to try every route to solve this problem, as there are 299 altogether! If you could check one trillion (1012) routes every second it would take over twenty billion years to check them all. There is an efficient algorithm to solve it. ;o)
解题思路:动态规划
import mathsqrt=math.sqrta=[]f = open("data.txt")line = f.readline()while line: row=line.split(' ') for i in range(0,len(row)): a.append(int(row[i])) line = f.readline()f.close()k=len(a)def getsun(x): redult=[]; k=int((sqrt(1+8*x)-1)/2)+1 return [x+k,x+k+1]def getrowstart(x): return int(x*(x-1)/2)for i in range(99,0,-1): start=getrowstart(i) for j in range(0,i): sun=getsun(start+j) a[start+j]+=max(a[sun[0]],a[sun[1]])print(a[0])
time: <1s
------------------
祝身体健康,万事如意
华电北风吹
天津大学计算机科学与技术学院
天津市卫津路92号
邮编: 300072
邮箱: 1194603539@qq.com
0 0
- EularProject 67: 三角形数字矩阵的最大路径
- EularProject 18: 三角形矩阵的最大路径
- 数字三角形的最大路径之及路径
- POJ1163—三角形矩阵最大路径
- 1730数字三角形问题(三角形内路径最大)
- EularProject 15: 方格迷宫的路径数
- 练习系统 实验三 数字三角形最大路径
- 算法学习之动态规划--数字三角形最大路径和
- 数字三角形最大和的计算
- 【DP】在矩阵中,选择一条从左上角到右下角、经过数字之和最大的路径
- 数字三角形+路径输出
- 动态规划(二)暴力递归的优化之路——数字三角形最大路径和
- 求矩阵的最大路径的问题
- 矩阵累积和的最大路径
- 求矩阵的最大和路径问题
- EularProject 47:具有相同个数因子的连续数字
- 矩阵路径最大和
- 【java】数字三角形最长路径
- 关于新浪云存储的权限问题Authorization
- 1036. Boys vs Girls (25)
- Java程序中的toString方法
- 动态代理(1)
- win7硬盘安装ubuntu经验
- EularProject 67: 三角形数字矩阵的最大路径
- 1037. Magic Coupon (25)
- nagios配置
- scala的协变与逆变、上界与下界
- 1038. Recover the Smallest Number (30)
- HDU2304 Electrical Outlets【水题】
- 1039. Course List for Student (25)
- JSON对象转换成JSON字符串
- Linux启动过程详解