poj1163 - DP递推、递归写法

本题超链接:http://poj.org/problem?id=1163

The Triangle

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 50977   Accepted: 30888

Description

7 3   8 8   1   0 2   7   4   4 4   5   2   6   5(Figure 1)

Figure 1 shows a number triangle. Write a program that calculates the highest sum of numbers passed on a route that starts at the top and ends somewhere on the base. Each step can go either diagonally down to the left or diagonally down to the right.

Input

Your program is to read from standard input. The first line contains one integer N: the number of rows in the triangle. The following N lines describe the data of the triangle. The number of rows in the triangle is > 1 but <= 100. The numbers in the triangle, all integers, are between 0 and 99.

Output

Your program is to write to standard output. The highest sum is written as an integer.

Sample Input

5
7
3 8
8 1 0
2 7 4 4
4 5 2 6 5

Sample Output

30

Source

IOI 1994

DP的递推写法

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <cmath>
 4 #include <iostream>
 5 using namespace std;
 6
 7 int dp[105][105];
 8 int f[105][105];
 9
10 int func(int n)
11 {
12     for(int i=n-1;i>=1;i--)
13     {
14         for(int j=1;j<=i;j++)
15         {
16             dp[i][j]=max(dp[i+1][j],dp[i+1][j+1])+f[i][j];
17         }
18     }
19     return dp[1][1];
20 }
21 int main()
22 {
23     int n;
24     while(~scanf("%d",&n))
25     {
26         for(int i=1;i<=n;i++)
27         {
28             for(int j=1;j<=i;j++)
29             {
30                 scanf("%d",&f[i][j]);
31             }
32         }
33         for(int i=1;i<=n;i++) dp[n][i]=f[n][i];
34         printf("%d\n",func(n));
35     }
36
37     return 0;
38 }

DP的递归写法

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <cmath>
 4 #include <iostream>
 5 using namespace std;
 6
 7 int dp[105][105];
 8 int f[105][105];
 9 int n;
10
11 int func(int i,int j)
12 {
13     if(i==n) return dp[i][j]=f[i][j];
14     if(dp[i][j]!=-1) return dp[i][j];
15     return dp[i][j]=max(func(i+1,j),func(i+1,j+1))+f[i][j];
16
17 }
18 int main()
19 {
20
21     while(~scanf("%d",&n))
22     {
23         memset(dp,-1,sizeof(dp));
24         for(int i=1;i<=n;i++)
25         {
26             for(int j=1;j<=i;j++)
27             {
28                 scanf("%d",&f[i][j]);
29             }
30         }
31
32         printf("%d\n",func(1,1));
33     }
34
35     return 0;
36 }
 
时间: 2024-08-09 02:19:03

poj1163 - DP递推、递归写法的相关文章

hdu2089(数位DP 递推形式)

不要62 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 25802    Accepted Submission(s): 8967 Problem Description 杭州人称那些傻乎乎粘嗒嗒的人为62(音:laoer).杭州交通管理局经常会扩充一些的士车牌照,新近出来一个好消息,以后上牌照,不再含有不吉利的数字了,这样一来,就可以

hdu 1207 汉诺塔II (DP+递推)

汉诺塔II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 4529    Accepted Submission(s): 2231 Problem Description 经典的汉诺塔问题经常作为一个递归的经典例题存在.可能有人并不知道汉诺塔问题的典故.汉诺塔来源于印度传说的一个故事,上帝创造世界时作了三根金刚石柱子,在一根柱子上从下往

CodeForces 372B 脑洞大开的DP递推

题目: 做了两个多小时,脑洞大开,给了一个01矩阵,求以a,b,为左上角,c,d为右下角的矩阵内有多少包含部分全为0的子矩阵 对于这道题目,一开始就想到了DP递推,感觉而已,虽然准,可是做不出啊,想好了递推式子可是细节部分没办法去处理.看了CF上的题解,顿时脑洞大开,这个做法真的是太厉害了,这方法代码简洁明了,同时也提醒到了我,在方程假设出来后,对于转移的细节处理, 其实一开始我想到过这个递推式子 dp[i][j][k][l] += dp[i][j][k - 1][l] + dp[i][j][k

D. Caesar&#39;s Legions 背包Dp 递推DP

http://codeforces.com/problemset/problem/118/D 设dp[i][j][k1][k2] 表示,放了i个1,放了j个2,而且1的连续个数是k1,2的连续个数是k2 如果这样写,用dfs写是很简单的.但是超时,我记忆化不到 如果用递推写,对于每一个状态,更新到下一个状态. 如果放的是1,那么新的状态是dp[i + 1][j][k1 + 1][0]也就是,用多了一个1,而且连续的个数也增加了.同时,2的连续个数就打破了,变成了0 这种枚举旧状态,更新下一个状态

HDU Tickets(简单的dp递推)

Tickets Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 972    Accepted Submission(s): 495 Problem Description Jesus, what a great movie! Thousands of people are rushing to the cinema. However,

递推递归小结

已经被递推递归虐了几天了,(┙>∧<)┙へ┻┻ 但是显而易见的是没有任何进展...作为一块并不Q弹好吃的连1116都不会写的蒟蒻突然饿了,我还能说什么呢. 所以只能给对面的一群大神跪了%%%%%%%%%%%%%orz 递推递归主要就是找思路,找到思路就并没有什么区别了. 但是我找不到思路啊 ╮(╯_╰)╭ 所以就找题解,但是递推专项找到的最齐的只有Pascal的代码,所以就看着Pascal的代码码了几道,于是到现在依然是明白了了几道题但是找不到规律一脸懵逼(=?ω?=)神圣与邪恶有汉化更新了呢

玲珑杯 #10 A dp递推

A. Black and White 题意:n个格子排在一行,每个格子里都有一枚白棋或一枚黑棋.限制:不能有连续a枚黑棋或连续b枚白棋,问有多少种方案. tags:一开始还以为是组合数学,没想到又是dp.这mmp的还是签到题 考虑长度为 i 的合法序列与长度为 i−1 的合法序列有什么关系.定dp[i][black] 为有 i 枚棋子且最后一枚是黑色的合法方案数量,g[i]为有 i 枚棋子的合法方案数量.现假设前面 i-1 枚棋都是合法的,在 i 位置加一枚黑棋,唯一不合法的情况就是最后的a枚棋

Luogu P1057 传球游戏(dp 递推)

P1057 传球游戏 题目描述 上体育课的时候,小蛮的老师经常带着同学们一起做游戏.这次,老师带着同学们一起做传球游戏. 游戏规则是这样的:n个同学站成一个圆圈,其中的一个同学手里拿着一个球,当老师吹哨子时开始传球,每个同学可以把球传给自己左右的两个同学中的一个(左右任意),当老师再次吹哨子时,传球停止,此时,拿着球没有传出去的那个同学就是败者,要给大家表演一个节目. 聪明的小蛮提出一个有趣的问题:有多少种不同的传球方法可以使得从小蛮手里开始传的球,传了m次以后,又回到小蛮手里.两种传球方法被视

hdu 1723 DP/递推

题意:有一队人(人数 ≥ 1),开头一个人要将消息传到末尾一个人那里,规定每次最多可以向后传n个人,问共有多少种传达方式. 这道题我刚拿到手没有想过 DP ,我觉得这样传消息其实很像 Fibonacci 所举的例子:一个人每次能够跨一或二阶台阶,问到 n 阶台阶有几种跨法.理念是一样的,只不过跨得台阶数可能会变而已.根据 Fibonacci 数列类比过来,每次最多能传 m 人,则 A [ i ] = A [ i - m ] + A [ i - m + 1 ] +  …… + A [ i - 1