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]

 1 def count_gold(pyramid):
 2     """
 3     Return max possible sum in a path from top to bottom
 4     """
 5
 6     step = len(pyramid)
 7
 8     sub_sum = [[0 for row in range(0, step)] for col in range(0, step)]
 9
10     sub_sum[0][0] = pyramid[0][0]
11
12     for i in range(0, step):
13         for j in range(0, i + 1):
14             if i >= 1:
15                 if j >= 1 and sub_sum[i - 1][j - 1] + pyramid[i][j] > sub_sum[i][j]:
16                     sub_sum[i][j] = sub_sum[i - 1][j - 1] + pyramid[i][j]
17
18                 if sub_sum[i - 1][j] + pyramid[i][j] > sub_sum[i][j]:
19                     sub_sum[i][j] = sub_sum[i - 1][j] + pyramid[i][j]
20
21     max_sum = 0
22     for each in sub_sum[step - 1][:step]:
23         if each > max_sum:
24             max_sum = each
25
26     #replace this for solution
27     return max_sum

其实这份代码类似C语言的实现

观摩zero_loss的python实现

1 def count_gold(pyramid):
2     py = [list(i) for i in pyramid]
3     for i in reversed(range(len(py)-1)):
4         for j in range(i+1):
5             py[i][j] +=(max(py[i+1][j], py[i+1][j+1]))
6
7     return py[0][0]

这个实现是从下往上走, 非常简洁;

Golden Pyramid

时间: 2024-11-09 03:02:50

Golden Pyramid的相关文章

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

在学习r-cnn系列时,一直看到SPP-net的身影,许多有疑问的地方在这篇论文里找到了答案. 论文:Spatial Pyramid Pooling in Deep Convolutional Networks for Visual Recognition 转自:http://blog.csdn.net/xzzppp/article/details/51377731 另可参考:http://zhangliliang.com/2014/09/13/paper-note-sppnet/ http:/

hdu 5432 Pyramid Split(二分搜索)

Problem Description Xiao Ming is a citizen who's good at playing,he has lot's of gold cones which have square undersides,let's call them pyramids. Anyone of them can be defined by the square's length and the height,called them width and height. To ea

hdoj 3820 Golden Eggs 【双二分图构造最小割模型】

Golden Eggs Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 505    Accepted Submission(s): 284 Problem Description There is a grid with N rows and M columns. In each cell you can choose to put

uva 11383 Golden Tiger Claw (KM算法)

uva 11383 Golden Tiger Claw 题目大意:给定一个N×N的矩阵,每个格子里都有一个正整数w(i,j).你的任务是给每行确定一个整数row(i), 每列也确定一个整数col(i),使得对于格子(i,j),w(i,j)<=row(i)+col(j).所有row(i)和col(j)的总和最小. 解题思路:KM算法. #include <cstdio> #include <cstring> #include <algorithm> #include

BNUOJ33568 Glass Pyramid(DFS)

Glass Pyramid Time Limit: 1000ms Memory Limit: 65536KB This problem will be judged on Ural. Original ID: 1968 64-bit integer IO format: %lld      Java class name: (Any) Prev Submit Status Statistics Discuss Next Font Size: +   - Type:   None  Graph T

Linear Spatial Pyramid Matching Using Sparse Coding for Image Classification

引入 Recently SVMs using spatial pyramid matching (SPM) kernel have been highly successful in image classification. Despite its popularity, these nonlinear SVMs have a complexity in training and O(n) in testing, where n is the training size, implying t

【Papers】Spatial Pyramid Matching

参考资料: [1]Lazebnik S, Schmid C, Ponce J. Beyond bags of features: Spatial pyramid matching for recognizing natural scene categories[C]//Computer Vision and Pattern Recognition, 2006 IEEE Computer Society Conference on. IEEE, 2006, 2: 2169-2178. [2]htt

HDOJ 4814 Golden Radio Base

利用题目中给出的公式和hint可以得到两个有用的公式: phi^(n) = phi^(n-1)+phi^(n-2) 2*(phi^n) = phi^(n+1)+phi^(n-2) 可以计算出phi^100远大于10^9,所以推测最后得到的phi进制的数整数和小数部分应该不会超过100位,事实表明,50位就能过. 所以最终变成了简单的模拟. Golden Radio Base Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 3276

multiboot 的golden image 和update image的 mcs文件的生成及调试

multiboot的功能对于不同的FPGA应该是相同的,但是具体的配置可能不一样.基本流程如下: 上图来源是xapp1246,  明显分为5个步骤,在FPGA启动时是有flash的0地址开始,但是因为golden image加了一些配置,在FPGA接收到这些配置是会转到其他的地址,继续读update image的内容.这些配置需要在生成golden bit的时候加约束命令如下: set_property BITSTREAM.GENERAL.COMPRESS TRUE [current_desig