【动态规划】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> # ...
随机推荐
- iOS Swift WisdomKeyboardKing 键盘智能管家SDK
iOS Swift WisdomKeyboardKing 键盘智能管家SDK [1]前言: 今天给大家推荐个好用的开源框架:WisdomKeyboardKing,方面iOS日常开发,优点和功能请 ...
- Java基础——注释规范
一.注释格式分类: 1.单行(single-line)注释://…… 2.块(block)注释:/*……*/ 3.文档注释:/**……*/ javadoc有如下: 二.加注释的场景: 1. 基本注释( ...
- mysql 导出表,导出数据 命令
mysql mysqldump 只导出表结构 不导出数据 复制代码代码如下: mysqldump --opt -d 数据库名 -u root -p > xxx.sql 备份数据库 复制代码代 ...
- 如何搭建openvpn
一.什么是openvpn Openvpn是一款基于openssl的开源vpn软件,它可以很好的运行在linux及windows各发行版本中,它的核心技术是虚拟网卡,其实它更像是一个底层的网卡驱动软件, ...
- Velocity学习4
Velocity是一个基于java的模板引擎(template engine).它允许任何人仅仅简单的使用模板语言(template language)来引用由java代码定义的对象. 当Veloci ...
- Keepalived高可用集群
一.服务介绍 keepalive起初是专为LVS设计的,专门用来监控LVS集群系统红各个服务节点的状态,后来又加入了VRRP的功能,因此不了配合LVS服务外,也可以作为其他服务(nginx,hapro ...
- 如何在ajax请求中设置特殊的RequestHeader
现在ajax应用已经相当广泛了,有很多不错的ajax框架可供使用.ajax是一个异步请求,也主要是一种客户端的脚本行为.那么,如何在请求之前为请求添加特殊的一些头部信息呢? 下面是一个简单的例子,我用 ...
- linux 问题收集
1,错误信息:bunzip2: command not found 解决方法:yum install -y bzip2 2,The X11 forwarding request was rejecte ...
- python爬取快手ios端首页热门视频
最近快手这种小视频app,特别的火,中午吃过午饭,闲来无聊,想搞下快手的短视频,看能不能搞到. 于是乎, 打开了fiddler,开始准备抓包, 设置代理,重启,下一步,查看本机ip 手机打开网络设置 ...
- 爬取代理IP
现在爬虫好难做啊,有些网站直接封IP,本人小白一个,还没钱,只能找免费的代理IP,于是去爬了西刺免费代理,结果技术值太低,程序还没调试好, IP又被封了... IP又被封了... IP又被封了... ...