【POJ 3176】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

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[i][j]表示第i行第j列的最大值

dp[i][j]=max{dp[i-1][j],dp[i-1][j-1]}+a[i][j].

#include<stdio.h>
#include<algorithm>
using namespace std;
int n,a[355][355],ans[355][355],maxans;
int main(){
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
        for(int j=1;j<=i;j++)
            scanf("%d",&a[i][j]);
    for(int i=1;i<=n;i++){
        for(int j=1;j<=i;j++){
            ans[i][j]=max(ans[i-1][j],ans[i-1][j-1])+a[i][j];
            maxans=max(ans[i][j],maxans);
        }
    }
    printf("%d",maxans);
    return 0;
}

逆向:逆着从n-1行到第1行,每次比较下方和右下方的大小,大的加上去,最后输出a[1][1]即可。

#include<stdio.h>
#include<algorithm>
using namespace std;
int n,i,j,a[355][355];
int main(){
    scanf("%d",&n);
    for(i=1;i<=n;i++)
        for(j=1;j<=i;j++)
            scanf("%d",&a[i][j]);
    for(i=n-1;i>=1;i--)
        for(j=1;j<=i;j++)
            a[i][j]+=max(a[i+1][j],a[i+1][j+1]);
    printf("%d",a[1][1]);
    return 0;
}
时间: 2024-10-06 07:22:49

【POJ 3176】Cow Bowling的相关文章

POJ 3176:Cow Bowling

Cow Bowling Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13464   Accepted: 8897 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

POJ 3176(Cow Bowling )(就是简单的数塔,动态规划)

Cow Bowling Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14206   Accepted: 9428 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

【POJ 3167】Cow Patterns (KMP+树状数组)

Cow Patterns Description A particular subgroup of K (1 <= K <= 25,000) of Farmer John's cows likes to make trouble. When placed in a line, these troublemakers stand together in a particular order. In order to locate these troublemakers, FJ has lined

【POJ 3270】Cow Sorting(置换群排序)

Cow Sorting(置换群排序) Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 6909 Accepted: 2716 Description Farmer John's N (1 ≤ N ≤ 10,000) cows are lined up to be milked in the evening. Each cow has a unique "grumpiness" level in the range 1

【POJ 3615】Cow Hurdles

Cow Hurdles Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6486   Accepted: 2948 Description Farmer John wants the cows to prepare for the county jumping competition, so Bessie and the gang are practicing jumping over hurdles. They are

【POJ 3267】 The Cow Lexicon

[POJ 3267] The Cow Lexicon 训练计划里把这题排到了topo里....然后我就这么死盯研究了一周topo算法(期间经历了三个人生风波....大物考试 高数考试跟模电考试----)啥不说了--上dp代码----没错 这是个dp!...赤果果的dp..就呢么傻呆呆地研究Topo算法--结果没研究出来.. 题意是给一个字符串和m个单词组成的字典,问最少删除几个字母能让这个字符串变成由字典中几个单词首位链接组成的字符串 dp思路还算好想 逆推 dp数组的下标是遍历字符串的起点 然

【POJ 1408】 Fishnet (叉积求面积)

[POJ 1408] Fishnet (叉积求面积) 一个1*1㎡的池塘 有2*n条线代表渔网 问这些网中围出来的最大面积 一个有效面积是相邻两行和相邻两列中间夹的四边形 Input为n 后面跟着四行 每行n个浮点数 每一行分别代表a,b,c,d 如图 并且保证a(i) > a(i-1) b(i) > b(i-1) c(i) > c(i-1) d(i) > d(i-1) n(n <= 30)*2+4(四个岸)条边 枚举点数就行 相邻的四个四个点枚举 找出围出的最大面积 找点用

【POJ 2513】Colored Sticks

[POJ 2513]Colored Sticks 并查集+字典树+欧拉通路 第一次做这么混的题..太混了-- 不过题不算难 字典树用来查字符串对应图中的点 每个单词做一个点(包括重复单词 题意就是每个边走且直走一次(欧拉通路 欧拉图的判定: 没有或者只有两个奇数度的点的图叫做欧拉图 有这些就可以解答此题了 另外需要注意题目范围是25W个木棍 所以最多可能有50W个点 卡了好多个RE 代码如下: #include <iostream> #include <cstdlib> #incl

2292: 【POJ Challenge 】永远挑战

2292: [POJ Challenge ]永远挑战 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 553  Solved: 230[Submit][Status][Discuss] Description lqp18_31和1tthinking经常出题来虐ftiasch.有一天, lqp18_31搞了一个有向图,每条边的长度都是1. 他想让ftiasch求出点1到点 N 的最短路."水题啊.", ftiasch这么说道. 所以1tth