【动态规划】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> # ...
随机推荐
- python学习之路(1)
今天刚入门python,对于有c和java基础的我,学习起来还是比较容易的,我并没有用PyCharm写,而是最基础的IDLE,学习python比java容易的地方就是不要写分号,不要打包,不要定义等等 ...
- JavaEE笔记(八)
第一个Spring Student(学生) bean package com.my.bean; import java.io.Serializable; public class Student im ...
- BZOJ1084_最大子矩阵_KEY
题目传送门 DP. 但要分类讨论,对于M=1和M=2的情况分别讨论. 1>M=1 设f[i][j]表示选了i个矩阵,到第j位.N^3转移.(前缀和) 2>M=2 设f[i][j][k]表示 ...
- python面试题(三)
一.项目技术点梳理 (一)Django项目 本项目是用python的Django框架开发的前后端不分离项目.项目采用MVT架构,使用的MySQL和redis数据库,用Django自带的orm与数据库交 ...
- Fortran的数组与指针
个人理解,欢迎指正 指针就是记录数据的内存地址的变量.指针可以指向单个变量,也可以指向数组. 数组是一个概念,是若干个类型相同的元素的有序集合.在Fortran语言中,数组中存放的元素,可以是整数,实 ...
- [PLC]ST语言一:LD_LDI_AND_ANI_OR_ORI
一:LD_LDI_AND_ANI_OR_ORI基本指令 说明:简单的顺控指令不做其他说明. 控制要求:无 编程梯形图: 结构化编程ST语言: M400:=(M0 OR M1) AND M2; M401 ...
- WebGL实现sprite精灵效果的GUI控件
threejs已经有了sprite插件,这就方便了three的用户,直接可以使用threejs的sprite插件来制作GUI模型.sprite插件是阿里的lasoy老师改造过的,这个很厉害,要学习一哈 ...
- Oracle中解析XMLType格式字段
背景:项目从某数据交换平台获取XML数据,以Oracle的XMLType格式保存在数据库字段中,需要建立触发器.存储过程,在保存数据时解析XML字段,将数据写入其他业务表中. 参考资料:Oracle的 ...
- 《Postgre SQL 即学即用 (第三版)》 分享 pdf下载
链接:https://pan.baidu.com/s/1akR33VqEkt99UqJUfiy2OA提取码:3p1k
- centos 开机自启设定:
在sentos系统下,主要有两种方法设置自己安装的程序开机启动.1.把启动程序的命令添加到/etc/rc.d/rc.local文件中,比如下面的是设置开机启动httpd. #!/bin/sh # # ...