Golden Pyramid
Golden Pyramid
Our Robo-Trio need to train for future journeys and treasure hunts. Stephan has built a special flat model of a pyramid. Now the robots can train for speed gold running. They start at the top of the pyramid and must collect gold in each room, choose to take the left or right path and continue down to the next level. To optimise their gold runs, Stephan need to know the maximum amount of gold that can be collected in one run.
Consider a tuple of tuples in which the first tuple has one integer and each consecutive tuple has one more integer then the last. Such a tuple of tuples would look like a triangle. You should write a program that will help Stephan find the highest possible sum on the most profitable route down the pyramid. All routes down the pyramid involve stepping down and to the left or down and to the right.
Tips: Think of each step down to the left as moving to the same index location or to the right as one index location higher. Be very careful if you plan to use recursion here.
Input: A pyramid as a tuple of tuples. Each tuple contains integers.
Output: The maximum possible sum as an integer.
题目大义: 数字金字塔, 找出从顶部到底部, 路径的最大和(即这条路径上的数字相加的和为最大)
经典的DP问题D[i][j] = max(D[i - 1][j], D[i - 1][j - 1]) + Pyramid[i][j]
def count_gold(pyramid):
"""
Return max possible sum in a path from top to bottom
""" step = len(pyramid) sub_sum = [[0 for row in range(0, step)] for col in range(0, step)] sub_sum[0][0] = pyramid[0][0] for i in range(0, step):
for j in range(0, i + 1):
if i >= 1:
if j >= 1 and sub_sum[i - 1][j - 1] + pyramid[i][j] > sub_sum[i][j]:
sub_sum[i][j] = sub_sum[i - 1][j - 1] + pyramid[i][j] if sub_sum[i - 1][j] + pyramid[i][j] > sub_sum[i][j]:
sub_sum[i][j] = sub_sum[i - 1][j] + pyramid[i][j] max_sum = 0
for each in sub_sum[step - 1][:step]:
if each > max_sum:
max_sum = each #replace this for solution
return max_sum
其实这份代码类似C语言的实现
观摩zero_loss的python实现
def count_gold(pyramid):
py = [list(i) for i in pyramid]
for i in reversed(range(len(py)-1)):
for j in range(i+1):
py[i][j] +=(max(py[i+1][j], py[i+1][j+1])) return py[0][0]
这个实现是从下往上走, 非常简洁;
Golden Pyramid的更多相关文章
- [Math]PHI, the golden ratio
PHI, the golden ratio 黄金分割比 转载自 http://paulbourke.net/miscellaneous/miscnumbers/ 1. Definition 将一个线段 ...
- CF 676B Pyramid of Glasses[模拟]
B. Pyramid of Glasses time limit per test 1 second memory limit per test 256 megabytes input standar ...
- Spatial pyramid pooling (SPP)-net (空间金字塔池化)笔记(转)
在学习r-cnn系列时,一直看到SPP-net的身影,许多有疑问的地方在这篇论文里找到了答案. 论文:Spatial Pyramid Pooling in Deep Convolutional Net ...
- 论文笔记之:Deep Generative Image Models using a Laplacian Pyramid of Adversarial Networks
Deep Generative Image Models using a Laplacian Pyramid of Adversarial Networks NIPS 2015 摘要:本文提出一种 ...
- Why The Golden Age Of Machine Learning is Just Beginning
Why The Golden Age Of Machine Learning is Just Beginning Even though the buzz around neural networks ...
- C Golden gun的巧克力
Time Limit:1000MS Memory Limit:65535K 题型: 编程题 语言: 无限制 描述 众所周知,13级有尊大神Golden gun,人称根叔,简称金枪!众立志进校队的 ...
- 10 Golden Rules of Project Risk Management
The benefits of risk management in projects are huge. You can gain a lot of money if you deal with u ...
- The golden ratio: 1.618
http://www.chinaz.com/design/2015/1109/467968_2.shtml The golden ratio: 1.618 a/b=b/(a+b) The Fibona ...
- codeforces 676B B. Pyramid of Glasses(模拟)
题目链接: B. Pyramid of Glasses time limit per test 1 second memory limit per test 256 megabytes input s ...
随机推荐
- sql中update,alter,modify,delete,drop的区别和使用(整理)(转)
关于update和alter: 百度知道上关于update和alter有一个很形象的总结: 一个表有很多字段,一个字段里有很多数据. 一个家有很多房间,一个房间里有很多家具. update是用来将衣柜 ...
- hdu 5072 Coprime (容斥)
Problem Description There are n people standing in a line. Each of them has a unique id number. Now ...
- pyqt学习之列表管理器(网友提供)
# -*- coding: utf-8 -*- __author__ = 'Administrator' import sys from PyQt4.QtCore import * from PyQt ...
- Dynamics CRM 2016 使用Plug-in Trace Log调试插件
1.写插件 首先,让我们写一个简单的插件来测试新插件跟踪日志功能.请注意,在下面的示例代码中,我们增加ITracingService的一个实例,以及记录有关插件的执行信息记录的一些键值: 2.注册插件 ...
- Oracle11g环境设置-windows环境
新建环境变量(系统变量),变量名:ORACLE_HOME 变量值:E:\app\Administrator\product\11.2.0\dbhome_1 新建环境变量(系统变量),变量名:ORACL ...
- openssl 非对称加密算法RSA命令详解
1.非对称加密算法概述 非对称加密算法也称公开密钥算法,其解决了对称加密算法密钥分配的问题,非对称加密算法基本特点如下: 1.加密密钥和解密密钥不同 2.密钥对中的一个密钥可以公开 3.根据公开密钥很 ...
- oracle数据库连接
///宁采花 8:37:39 /// <summary> /// 获取数据链接 /// </summary> /// <returns></returns&g ...
- http断点续传原理
断点续传一是断点,一续传. 断点是在下载时,将下载文件分多片,同时进行多片一起下载,如果任务被暂停,暂停的位置就是断点. 续传就是未完成的下载再次开始时,会从上次的断点继续传送. 在下载(或上传)过程 ...
- RadGrid SelectedIndexChanged 事件没反应的解决方法
Hello Hrushikesh, You can set ClientSettings.EnablePostBackOnRowClick to true along with ClientSetti ...
- poj3258 二分 最小值最大化问题
River Hopscotch Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10842 Accepted: 4654 ...