【动态规划】POJ-3176
一、题目
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的更多相关文章
- POJ 3176 Cow Bowling(dp)
POJ 3176 Cow Bowling 题目简化即为从一个三角形数列的顶端沿对角线走到底端,所取得的和最大值 7 * 3 8 * 8 1 0 * 2 7 4 4 * 4 5 2 6 5 该走法即为最 ...
- poj 1163 The Triangle &poj 3176 Cow Bowling (dp)
id=1163">链接:poj 1163 题意:输入一个n层的三角形.第i层有i个数,求从第1层到第n层的全部路线中.权值之和最大的路线. 规定:第i层的某个数仅仅能连线走到第i+1层 ...
- POJ - 3176 Cow Bowling 动态规划
动态规划:多阶段决策问题,每步求解的问题是后面阶段问题求解的子问题,每步决策将依赖于以前步骤的决策结果.(可以用于组合优化问题) 优化原则:一个最优决策序列的任何子序列本身一定是相当于子序列初始和结束 ...
- poj 3176 Cow Bowling(区间dp)
题目链接:http://poj.org/problem?id=3176 思路分析:基本的DP题目:将每个节点视为一个状态,记为B[i][j], 状态转移方程为 B[i][j] = A[i][j] + ...
- 二分+动态规划 POJ 1973 Software Company
Software Company Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 1112 Accepted: 482 D ...
- POJ 3176 Cow Bowling
Cow Bowling Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13016 Accepted: 8598 Desc ...
- POJ 3176 简单DP
Cow Bowling Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16448 Accepted: 10957 Descrip ...
- [ACM_动态规划] POJ 1050 To the Max ( 动态规划 二维 最大连续和 最大子矩阵)
Description Given a two-dimensional array of positive and negative integers, a sub-rectangle is any ...
- 【POJ 3176】Cow Bowling
题 Description The cows don't use actual bowling balls when they go bowling. They each take a number ...
- DP:Cow Bowling(POJ 3176)
北大教你怎么打保龄球 题目很简单的,我就不翻译了,简单来说就是储存每一行的总数,类似于状态压缩 #include <stdio.h> #include <stdlib.h> # ...
随机推荐
- Kotlin安卓页面本地存储数据(方法和封装)
直接上代码 封装: //存储key对应的数据 fun saveData(context: Activity, key: String, info: String) { val sharedPrefer ...
- Matlab中的“prod”函数
B = prod(A)将A矩阵不同维的元素的乘积返回到矩阵B. 如果A是向量,prod(A)返回A向量的乘积.如果A是矩阵,prod(A)将A看作列向量,返回每一列元素的乘积并组成一个行向量B.如果A ...
- 大数据入门第十天——hadoop高可用HA
一.HA概述 1.引言 正式引入HA机制是从hadoop2.0开始,之前的版本中没有HA机制 2.运行机制 实现高可用最关键的是消除单点故障 hadoop-ha严格来说应该分成各个组件的HA机制——H ...
- # 20155229 2016-2017-2 《Java程序设计》第七周学习总结
20155229 2016-2017-2 <Java程序设计>第七周学习总结 教材学习内容总结 第十二章 lambda的主体是一个表达式,而不是一个代码块.仅仅能在lambda表达式中封装 ...
- 2017-2018-1 20155318 《信息安全系统设计基础》第九周课下实践——实现mypwd
2017-2018-1 20155318 <信息安全系统设计基础>第九周课下实践--实现mypwd 相关知识 man -k 查找含有关键字的内容 与管道命令结合使用:man -k k1 | ...
- Deep Learning综述[上]
Deep-Learning-Papers-Reading-Roadmap: [1] LeCun, Yann, Yoshua Bengio, and Geoffrey Hinton. "Dee ...
- log4j.properties配置文件详解
Log4J的配置文件(Configuration File)就是用来设置记录器的级别.存放器和布局的,它可接key=value格式的设置或xml格式的设置信息.通过配置,可以创建出Log4J的运行环境 ...
- 【转载】COM 组件设计与应用(五)——用 ATL 写第一个组件
原文:http://vckbase.com/index.php/wv/1215.html 一.前言 1.如果你在使用 vc5.0 及以前的版本,请你升级为 vc6.0 或 vc.net 2003: 2 ...
- setjmp/longjmp 处理异常
#include <stdio.h> #include <stdlib.h> #include <setjmp.h> jmp_buf jb; void f1() { ...
- 【JUC源码解析】DelayQueue
简介 基于优先级队列,以过期时间作为排序的基准,剩余时间最少的元素排在队首.只有过期的元素才能出队,在此之前,线程等待. 源码解析 属性 private final transient Reentra ...