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的更多相关文章

  1. [Math]PHI, the golden ratio

    PHI, the golden ratio 黄金分割比 转载自 http://paulbourke.net/miscellaneous/miscnumbers/ 1. Definition 将一个线段 ...

  2. CF 676B Pyramid of Glasses[模拟]

    B. Pyramid of Glasses time limit per test 1 second memory limit per test 256 megabytes input standar ...

  3. Spatial pyramid pooling (SPP)-net (空间金字塔池化)笔记(转)

    在学习r-cnn系列时,一直看到SPP-net的身影,许多有疑问的地方在这篇论文里找到了答案. 论文:Spatial Pyramid Pooling in Deep Convolutional Net ...

  4. 论文笔记之:Deep Generative Image Models using a Laplacian Pyramid of Adversarial Networks

    Deep Generative Image Models using a Laplacian Pyramid of Adversarial Networks NIPS 2015  摘要:本文提出一种 ...

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

  6. C Golden gun的巧克力

    Time Limit:1000MS  Memory Limit:65535K 题型: 编程题   语言: 无限制 描述 众所周知,13级有尊大神Golden gun,人称根叔,简称金枪!众立志进校队的 ...

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

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

  9. codeforces 676B B. Pyramid of Glasses(模拟)

    题目链接: B. Pyramid of Glasses time limit per test 1 second memory limit per test 256 megabytes input s ...

随机推荐

  1. BOT、BT、PPP形式介绍(2)

    BT1.什么是BT     BT投资是BOT的一种变换形式,即Build-Transfer(建设—转让),政府通过特许协议,引入国外资金或民间资金进行专属于政府的基础设施建设,基础设施建设完工后,该项 ...

  2. win10 iis 创建新站点注意事项

    新建站点时:注意文件夹权限增加everyone. 快速打开IIS:win+r:inetmgr

  3. centos Minicom通信终端

    minicom是linux下的串口通信软件,他使用完全使用键盘操作.它虽然没有windows下的超级终端好用,但是它也是一种串口通信的方法.一.minicom安装在超级终端中输入:sally@sall ...

  4. eclipse3.7 安装maven插件与scm

    转自:http://blacksonny.iteye.com/blog/1900275 最近要使用maven进行开发,之前的eclipse3.7 使用一下两个地址安装好了插件,如下: maven插件 ...

  5. Linux文件io--one简述

    什么是文件IO IO就是input/output,输入/输出.文件IO的意思就是读写文件. 文件操作的主要接口API 什么是操作系统API (1)API是一些函数,这些函数是由linux系统提供支持的 ...

  6. poj 2049 Let it Bead(polya模板)

      Description Cannery Row percent of the target audience insists that the bracelets be unique. (Just ...

  7. C/C++经典面试题目

    1.关于动态申请内存 答:内存分配方式三种: (1)从静态存储区域分配:内存在程序编译的时候就已经分配好,这块内存在程序的整个运行期间都存在.全局变量,static变量. (2)在栈上创建:在执行函数 ...

  8. C++ MPICH

    假设一个C++的MPI程序在单机上能够跑.可是在多机上跑会报下面错误: Fatal error in MPI_Send: Unkown error class , error stack. 解决方法: ...

  9. HDU1285——确定比赛名次

    Problem Description 有N个比赛队(1<=N<=500),编号依次为1,2,3,....,N进行比赛,比赛结束后,裁判委员会要将所有参赛队伍从前往后依次排名,但现在裁判委 ...

  10. centos6.5配置无线网络

    由于安装的是服务器版,所以开机无法连接网络,以下这些情况都是针对驱动已经安装OK.按步骤操作如下(以下操作默认都是在超级管理员权限下进行): 1.测试电脑是否安装wpa_supplicant,测试方法 ...