poj3176 Cow Bowling
来源:互联网 发布:docker sql server 编辑:程序博客网 时间:2024/05/18 01:56
Cow Bowling
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 13069 Accepted: 8624
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:
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:
The highest score is achievable by traversing the cows as shown above.
Source
USACO 2005 December Bronze
======================================================================================
递归 + 数据记录以减少计算量(Memo-ization, top down)
#include <cstdio>#define MAXN 360int InputMat[MAXN][MAXN];//输入矩阵int MaxSumAchievable[MAXN][MAXN];int n;//输入数据的行数int myMax(int a, int b){return (a>b)?a:b;}//MaxSumAchievable有可能是0,所以要初始化为负void init(){for (int i=0; i<MAXN; ++i){for (int j=0; j<MAXN; ++j){MaxSumAchievable[i][j]=-1;}}}//递归+数据记录//设f[i][j]为点(i, j)所能有的最大路径和,用一个数组MaxSumAchievable记录已经算出的结果int f(int i, int j){if(i==n-1){MaxSumAchievable[i][j]=InputMat[i][j];return MaxSumAchievable[i][j];//最后一行,f值是输入矩阵元素本身,写入记录并返回}if(MaxSumAchievable[i][j]>=0) return MaxSumAchievable[i][j];//已经被计算好的记录,返回这个记录//若没有记录,用递归函数计算MaxSumAchievable[i][j] = InputMat[i][j] + myMax(f(i+1, j), f(i+1, j+1));//回溯的时候可写入return MaxSumAchievable[i][j];}int main(){int m=0;scanf("%d", &n);for (int j=0; j<n; ++j){for (int i=0; i<=m; ++i){scanf("%d", &InputMat[m][i]);}++m;}init();printf("%d\n", f(0, 0));return 0;}
======================================================================================
循环,自底向上计算(Tabulation, bottom up)
#include <cstdio>#define MAXN 360int a[MAXN][MAXN];//输入数组int mymax(int a, int b){return (a>b)?a:b;}int main(){int n;scanf("%d", &n);for (int i=0; i<n; ++i){for (int j=0; j<i+1; ++j){scanf("%d", &a[i][j]);}}//bottom-up tabulationfor (int i=n-2; i>=0; --i){for (int j=0; j<i+1; ++j){a[i][j] += mymax(a[i+1][j], a[i+1][j+1]);}}printf("%d\n", a[0][0]);return 0;}======================================================================================
递归打印路径
#include <cstdio>#define MAXN 360typedef struct _arr{int info;bool isWP;//means "is way point"} arr;arr a[MAXN][MAXN];//输入数组int dp[MAXN][MAXN];//存放答案的数组//递归打印路径void PrintRoute(int i, int j, int n){if(i==n-1)//递归出口{printf("%d\n", a[i][j]);}else{printf("%d -> ", a[i][j]);if(a[i+1][j].isWP) PrintRoute(i+1, j, n);else PrintRoute(i+1, j+1, n);}}int main(){freopen("D:\\in.txt", "r", stdin);freopen("D:\\out.txt", "w", stdout);int n;scanf("%d", &n);//读入for (int i=0; i<n; ++i){for (int j=0; j<i+1; ++j){scanf("%d", &(a[i][j].info)); a[i][j].isWP=false;}}//初始化dp数组for (int i=0; i<n; ++i){dp[n-1][i] = a[n-1][i].info;}//bottom-up tabulationfor (int i=n-2; i>=0; --i){for (int j=0; j<i+1; ++j){if (dp[i+1][j]>dp[i+1][j+1]){dp[i][j] = a[i][j].info + dp[i+1][j];a[i+1][j].isWP=true;}else{dp[i][j] = a[i][j].info + dp[i+1][j+1];a[i+1][j+1].isWP=true;}}}printf("%d\n", dp[0][0]);//打印结果PrintRoute(0, 0, n);//递归打印路径return 0;}
结果:
- POJ3176----Cow Bowling
- poj3176 Cow Bowling DP
- poj3176 cow bowling
- poj3176 Cow Bowling
- poj3176 Cow Bowling
- poj3176 Cow Bowling
- [dp]poj3176 Cow Bowling
- POJ3176--Cow Bowling
- POJ3176-Cow Bowling
- [DP]poj3176 Cow Bowling
- POJ3176-Cow Bowling
- POJ3176 Cow Bowling
- POJ3176-Cow Bowling(dp)
- POJ3176——Cow Bowling
- POJ3176-Cow Bowling-简单dp
- poj3176——cow bowling
- poj3176--Cow Bowling(dp:数塔问题)
- poj3176——Cow Bowling(dp)
- 自动1122徐达武第六章剩下作业
- HDU 3341 Lost's revenge(AC自动机+DP)
- 【LeetCode】Convert Sorted Array to Binary Search Tree
- 教你如何选购网络摄像头
- CocacChina 测试题 二
- poj3176 Cow Bowling
- 【DP】HDU1950 Bridging signals——LIS最大上升子序列算法
- python安装setuptools(easy_install工具)
- 子容器垂直居中于父容器的方案
- 代码面试最常用的10大算法
- 僵尸进程和处理
- 计划未来几周
- 移动应用研发经验记录与分享一
- 05LDAP目录服务