POJ3176——Cow Bowling(动态规划)
Cow Bowling
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
题目大意:
输入一个n层的三角形,第i层有i个数,求从第1层到第n层的所有路线中,权值之和最大的路线。
规定:第i层的某个数只能连线走到第i+1层中与它位置相邻的两个数中的一个。
解题思路:
动态规划。
dp[i][j]=max(dp[i-1][j],dp[i-1][j-1]+data[i][j]
因为dp数组的值只与上一行有关,用滚动数组优化了一下。(直接开成一维的了。。。)
Code(Mem:1184K Tim:32MS):
/*************************************************************************
> File Name: poj3176.cpp
> Author: Enumz
> Mail: 369372123@qq.com
> Created Time: 2014年10月21日 星期二 19时38分18秒
************************************************************************/
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<list>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
#define MAXN 351
using namespace std;
int way[MAXN][MAXN],dp[MAXN][MAXN];
int N;
void Input()
{
cin>>N;
for (int i=;i<=N;i++)
for (int j=;j<=i;j++)
scanf("%d",&way[i][j]);
}
void Solve()
{
memset(dp,,sizeof(dp));
for (int i=;i<=N;i++)
for (int j=;j<=i;j++)
dp[i][j]=max(dp[i-][j],dp[i-][j-])+way[i][j];
}
void Output()
{
int ret=dp[N][];
for (int i=;i<=N;i++)
if (ret<dp[N][i]) ret=dp[N][i];
cout<<ret<<endl;
}
int main()
{
Input();
Solve();
Output();
return ;
}
Code(滚动数组优化 Mem:224K Tim:47MS):
/*************************************************************************
> File Name: poj3176.cpp
> Author: Enumz
> Mail: 369372123@qq.com
> Created Time: 2014年10月21日 星期二 19时38分18秒
************************************************************************/
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<list>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
#define MAXN 351
using namespace std;
int way[MAXN],dp[MAXN];
int N;
void Input()
{
cin>>N;
memset(dp,,sizeof(dp));
for (int i=; i<=N; i++)
{
for (int j=; j<=i; j++)
scanf("%d",&way[j]);
for (int j=i; j>=; j--)
dp[j]=max(dp[j],dp[j-])+way[j];
}
}
void Output()
{
int ret=dp[];
for (int i=; i<=N; i++)
if (ret<dp[i]) ret=dp[i];
cout<<ret<<endl;
}
int main()
{
Input();
Output();
return ;
}
POJ3176——Cow Bowling(动态规划)的更多相关文章
- POJ3176 Cow Bowling 2017-06-29 14:33 23人阅读 评论(0) 收藏
Cow Bowling Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 19173 Accepted: 12734 Des ...
- Poj3176 Cow Bowling (动态规划 数字三角形)
Description The cows don't use actual bowling balls when they go bowling. They each take a number (i ...
- POJ - 3176 Cow Bowling 动态规划
动态规划:多阶段决策问题,每步求解的问题是后面阶段问题求解的子问题,每步决策将依赖于以前步骤的决策结果.(可以用于组合优化问题) 优化原则:一个最优决策序列的任何子序列本身一定是相当于子序列初始和结束 ...
- poj-3176 Cow Bowling &&poj-1163 The Triangle && hihocoder #1037 : 数字三角形 (基础dp)
经典的数塔模型. 动态转移方程: dp[i][j]=max(dp[i+1][j],dp[i+1][j+1])+p[i][j]; #include <iostream> #include ...
- POJ3176:Cow Bowling(数字三角形问题)
地址:http://poj.org/problem?id=3176 题目解析:没什么好说的,之前上课时老师讲过.从下往上找,每一个三角形的顶点可由两个角加上顶点的值 两种方式得到 ,用dp数组保存下最 ...
- 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 3176 Cow Bowling
Cow Bowling Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13016 Accepted: 8598 Desc ...
- Cow Bowling
Cow Bowling Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 15585 Accepted: 10363 Descrip ...
- POJ 3176:Cow Bowling
Cow Bowling Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13464 Accepted: 8897 Desc ...
随机推荐
- java 顺序表
想看看java版的数据结构,了解一下树的一些操作,写了个顺序表熟悉一下 package com.sqlist; /** * @author xiangfei * 定义一个顺序表 * */ public ...
- 安装配置Apache2.4和php7.0
接下来就要进入到PHP的学习了,所以要安装Apache服务器和PHP,从昨天开始一直到刚刚才配置完成,中间也遇到了一些问题,所以整理一下写了下来.接下来就是Win64位系统配置Apache2.4和PH ...
- 防止IE7,8进入怪异模式
在页头添加 <meta http-equiv="X-UA-Compatible" content="IE=edge" />
- Matlab实现movieLens转矩阵
for mm=1:num_m %电影编号是mm的训练集行号 ff= find(train_vec(:,2)==mm); %train_vec(ff,1) 行号对应的用户编号 count(train_v ...
- A beginner’s introduction to Deep Learning
A beginner’s introduction to Deep Learning I am Samvita from the Business Team of HyperVerge. I join ...
- 鼠标滚轮事件MouseWheel
其实在大多数浏览器(IE6, IE7, IE8, Opera 10+, Safari 5+,Chrome)中,都提供了 "mousewheel" 事件.但杯具的是 Firefox ...
- ZOJ3554 A Miser Boss(dp)
给你n个工件,然后有A,B,C三个工厂,然后它们加工第i个工件所需要的时间分别为a[i],b[i],c[i],然后现在要你利用三间工厂加工所有的零件,要求是任何时间工厂都不能停工,而且一定要三间同时做 ...
- POJ 2674
Linear world Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 2448 Accepted: 564 Descr ...
- Asp.net最基本的文件上传功能代码
aspx前台页面代码 <form id="form1" action="File.aspx" runat="server" encty ...
- 项目中用到的SQL-总结
基本sql总结: Group by的理解:having子句,分组函数 Group by使用的限定: 1.出现在Select列表中的字段或者出现在order by后面的字段,如果不是包含在分组函数中,那 ...