【动态规划】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> # ...
随机推荐
- html 头标签 <meta http-equiv 属性应用。
比较常用的 1. 网页出现乱码时设置字符集 <meta http-equiv="content-Type" content="text/html; charset= ...
- 基于Babylon.js编写简单的骨骼动画生成器
使用骨骼动画技术可以将网格的顶点分配给若干骨头,通过给骨头设定关键帧和父子关系,可以赋予网格高度动态并具有传递性的变形 效果.这里结合之前的相关研究在网页端使用JavaScript实现了一个简单的骨骼 ...
- linux命令--cp、tail、cd、mv、history、cd
day1 cd命令 cd ../定位至上级目录 cd ./定位到当前目录 cd ~ 定位当前用户目录 cd / 定位系统根目录 cd - 返回进入此目录之前所在的目录 day2 mv命令: mv时,若 ...
- Matlab 装自定义模块
Matlab for Mac 右上角有一个set path选项. 点进去再点击 add with subfolders. 把你下载好的且解压过的工具箱添加进去 然后点save. 重启,就可以直接用了.
- 9 ORM-高阶补充(未完成)
https://www.cnblogs.com/alice-bj/p/9195846.html#_labelTop https://www.cnblogs.com/yuanchenqi/article ...
- 2、rbac组件 后台布局模板,权限按钮,菜单,可拔插,路径重定向
1.后台布局管理 https://www.cnblogs.com/venicid/p/7772742.html#_label0 1.通用模板 overflow: auto; //在a和b模板中进行切换 ...
- 【LG3245】[HNOI2016]大数
[LG3245][HNOI2016]大数 题面 洛谷 题解 60pts 拿vector记一下对于以每个位置为右端点符合要求子串的左端点, 则每次对于一个询问,扫一遍右端点在vector里面二分即可, ...
- asp.net core 发布到docker 极简步骤
1.使用dotnet命令发布项目 2.把发布成功的文件通过scp等工具发布到linux服务器上,在当前目录下新建一个dockerfile 3.使用asp.net core镜像为底包构建一个新的镜像 4 ...
- 人工智能AI芯片与Maker创意接轨 (上)
近几年来人工智能(Artificial Intelligence, AI)喴的震天价响,吃也要AI,穿也要AI,连上个厕所也要来个AI智能健康分析,生活周遭食衣住行育乐几乎无处不AI,彷佛已经来到科幻 ...
- 【Shader】这是一篇记录随笔,我要开始学Shader了!
背景: 前天学校有场招聘会,转了一圈只看到一家和unity有关的公司,还是做VR游戏的,然后HR也很好说话.和我说话的HR正好是做UnityVR方面的,聊了会受益匪浅,自己就像是找到了方向似的,突然很 ...