一、题目

Description

The cows don't use actual bowling balls when they go bowling. They each take a number (in the range 0..99), though, and line up in a standard bowling-pin-like triangle like this:

        7
3 8
8 1 0
2 7 4 4
4 5 2 6 5

Then the other cows traverse the triangle starting from its tip and moving "down" to one of the two diagonally adjacent cows until the "bottom" row is reached. The cow's score is the sum of the numbers of the cows visited along the way. The cow with the highest score wins that frame.

Given a triangle with N (1 <= N <= 350) rows, determine the highest possible sum achievable.

Input

Line 1: A single integer, N

Lines 2..N+1: Line i+1 contains i space-separated integers that represent row i of the triangle.

Output

Line 1: The largest sum achievable using the traversal rules

Sample Input

5

7

3 8

8 1 0

2 7 4 4

4 5 2 6 5

Sample Output

30

Hint

Explanation of the sample:

        7
*
3 8
*
8 1 0
*
2 7 4 4
*
4 5 2 6 5

The highest score is achievable by traversing the cows as shown above.

二、思路&心得

​简单的动态规划问题,从底向上依次扫描就行了,可以用直接用DP,也可以用记忆化搜索。

三、代码

1.DP:

#include<cstdio>
#include<algorithm>
using namespace std;
const int MAX_N = 355; int N;
int dp[MAX_N][MAX_N]; void solve() {
for (int i = 0; i < N; i ++) {
for (int j = 0; j <= i; j ++) {
scanf("%d", &dp[i][j]);
}
}
for (int i = N - 2; i >=0; i --) {
for (int j = 0; j <= i; j ++) {
dp[i][j] += max(dp[i + 1][j], dp[i + 1][j + 1]);
}
}
printf("%d\n", dp[0][0]);
} int main() {
while (~scanf("%d", &N)) {
solve();
}
return 0;
}

2.记忆化搜索:

#include<cstdio>
#include<algorithm>
using namespace std;
const int MAX_N = 355; int N;
int dp[MAX_N][MAX_N];
int visit[MAX_N][MAX_N]; int score(int x, int y) {
if (visit[x][y] == 1) return dp[x][y];
visit[x][y] = 1;
if (x == N - 1) return dp[x][y];
return dp[x][y] += max(score(x + 1, y), score(x + 1, y + 1));
} void solve() {
for (int i = 0; i < N; i ++) {
for (int j = 0; j <= i; j ++) {
scanf("%d", &dp[i][j]);
}
}
printf("%d\n", score(0, 0));
} int main() {
while (~scanf("%d", &N)) {
solve();
}
return 0;
}

【动态规划】POJ-3176的更多相关文章

  1. POJ 3176 Cow Bowling(dp)

    POJ 3176 Cow Bowling 题目简化即为从一个三角形数列的顶端沿对角线走到底端,所取得的和最大值 7 * 3 8 * 8 1 0 * 2 7 4 4 * 4 5 2 6 5 该走法即为最 ...

  2. poj 1163 The Triangle &amp;poj 3176 Cow Bowling (dp)

    id=1163">链接:poj 1163 题意:输入一个n层的三角形.第i层有i个数,求从第1层到第n层的全部路线中.权值之和最大的路线. 规定:第i层的某个数仅仅能连线走到第i+1层 ...

  3. POJ - 3176 Cow Bowling 动态规划

    动态规划:多阶段决策问题,每步求解的问题是后面阶段问题求解的子问题,每步决策将依赖于以前步骤的决策结果.(可以用于组合优化问题) 优化原则:一个最优决策序列的任何子序列本身一定是相当于子序列初始和结束 ...

  4. poj 3176 Cow Bowling(区间dp)

    题目链接:http://poj.org/problem?id=3176 思路分析:基本的DP题目:将每个节点视为一个状态,记为B[i][j], 状态转移方程为 B[i][j] = A[i][j] + ...

  5. 二分+动态规划 POJ 1973 Software Company

    Software Company Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 1112   Accepted: 482 D ...

  6. POJ 3176 Cow Bowling

    Cow Bowling Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13016   Accepted: 8598 Desc ...

  7. POJ 3176 简单DP

    Cow Bowling Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16448 Accepted: 10957 Descrip ...

  8. [ACM_动态规划] POJ 1050 To the Max ( 动态规划 二维 最大连续和 最大子矩阵)

    Description Given a two-dimensional array of positive and negative integers, a sub-rectangle is any ...

  9. 【POJ 3176】Cow Bowling

    题 Description The cows don't use actual bowling balls when they go bowling. They each take a number ...

  10. DP:Cow Bowling(POJ 3176)

    北大教你怎么打保龄球 题目很简单的,我就不翻译了,简单来说就是储存每一行的总数,类似于状态压缩 #include <stdio.h> #include <stdlib.h> # ...

随机推荐

  1. 配置bond

    注意:配置bond要有两个以上的网口 1.配置文件所有目录:/etc/sysconfig/network-scripts 网口配置文件名规则:以ifcfg-开头,然后接着是网口名 例如:eth0的配置 ...

  2. SpringBoot与mongodb的结合

    本文系列文章: ​ 使用Shell 操作 MongoDB的技巧 ​ MongoTemplate的使用技巧及其注意事项 敬请期待. 前言 最近公司想要做一个用户行为数据的收集,最开始想用mysql来存储 ...

  3. goalng nil interface浅析

    0.遇到一个问题 代码 func GetMap (i interface{})(map[string]interface{}){ if i == nil { //false ??? i = make( ...

  4. JavaEE笔记(六)

    实现Action的几种方法1. implements Action2. extends ActionSupport3. 也可以不继承任何父类不实现任何借口 #当一个类有多个方法 package com ...

  5. Noip前的大抱佛脚----赛前任务

    赛前任务 tags:任务清单 前言 现在xzy太弱了,而且他最近越来越弱了,天天被爆踩,天天被爆踩 题单不会在作业部落发布,所以可(yi)能(ding)会不及时更新 省选前的练习莫名其妙地成为了Noi ...

  6. python基础学习1-装饰器在登陆模块应用

    LOGIN_USER ={"islogin":False} def outer(func): def inner(*args,**kwargs): if LOGIN_USER[&q ...

  7. python爬虫之Scrapy框架(CrawlSpider)

    提问:如果想要通过爬虫程序去爬取”糗百“全站数据新闻数据的话,有几种实现方法? 方法一:基于Scrapy框架中的Spider的递归爬去进行实现的(Request模块回调) 方法二:基于CrawlSpi ...

  8. 新建一个Java Web程序

    依次选择 File——New——Web——Dynamic Web Project 输入项目名称“MyWebProject”,选择好Apache Tomcat V9.0服务器,其他采用默认配置. 单击N ...

  9. 使用jquery ajax代替iframe

    大家在实际编写网页时可能会遇到网页中需要嵌套网页的情况,这时候通常想法就是通过iframe标签. 但实际用过的人都知道其有种种的不方便,比较直观的问题就是iframe的自适应高度,这也是处理起来比较麻 ...

  10. Python学习过程笔记整理(一)

    编码方式 -Utf8编码方式:# -*- coding: utf-8 -*- 注释 -行注释 # -块注释 '''...'''或"""...""&qu ...