HDU 2391 Filthy Rich (dp)
Problem Description
They say that in Phrygia, the streets are paved with gold. You’re currently on vacation in Phrygia, and to your astonishment you discover that this is to be taken literally: small heaps of gold are distributed throughout the city. On a certain day, the Phrygians even allow all the tourists to collect as much gold as they can in a limited rectangular area. As it happens, this day is tomorrow, and you decide to become filthy rich on this day. All the other tourists decided the same however, so it’s going to get crowded. Thus, you only have one chance to cross the field. What is the best way to do so?
Given a rectangular map and amounts of gold on every field, determine the maximum amount of gold you can collect when starting in the upper left corner of the map and moving to the adjacent field in the east, south, or south-east in each step, until you end up in the lower right corner.
Input
The input starts with a line containing a single integer, the number of test cases.
Each test case starts with a line, containing the two integers r and c, separated by a space (1 <= r, c <= 1000). This line is followed by r rows, each containing c many integers, separated by a space. These integers tell you how much gold is on each field. The amount of gold never negative.
The maximum amount of gold will always fit in an int.
Output
For each test case, write a line containing “Scenario #i:”, where i is the number of the test case, followed by a line containing the maximum amount of gold you can collect in this test case. Finish each test case with an empty line.
Sample Input
1
3 4
1 10 8 8
0 0 1 8
0 27 0 4
Sample Output
Scenario #1:
42
分析:
首行给出T,代表有T组数据。每组数据第一行给出n,m代表接着有n行m列的数据。起点在左上角,终点在右下角,每一步可以向下向右向右下走,输出途中最大数字和。
思路:动态规划,dp[i][j]代表到达(i,j)能获取的最大数字和
状态转移方程
dp[i][j]=max(dp[i][j],dpi][j-1]+tu[i][j]),j-1>=0;
dp[i][j]=max(dp[i][j],dp[i-1][j]+tu[i][j]),i-1>=0;
dp[i][j]=max(dp[i][j],dp[i-1][j-1]+tu[i][j]),i-1>=0,j-1>=0;
代码:
#include<stdio.h>
#include<iostream>
#include<string.h>
using namespace std;
int tu[1010][1010];
int dp[1010][1010];
int main()
{
int T;
scanf("%d",&T);
for(int t=1;t<=T;t++)
{
int n,m;
scanf("%d%d",&n,&m);
for(int i=0; i<n; i++)
{
for(int j=0; j<m; j++)
scanf("%d",&tu[i][j]);
}
memset(dp,0,sizeof(dp));
dp[0][0]=tu[0][0];
for(int i=0; i<n; i++)
for(int j=0; j<m; j++)
{
if(j-1>=0&&dp[i][j]<dp[i][j-1]+tu[i][j])
dp[i][j]=dp[i][j-1]+tu[i][j];
if(i-1>=0&&dp[i][j]<dp[i-1][j]+tu[i][j])
dp[i][j]=dp[i-1][j]+tu[i][j];
if(j-1>=0&&i-1>=0&&dp[i][j]<dp[i-1][j-1]+tu[i][j])
dp[i][j]=dp[i-1][j-1]+tu[i][j];
}
printf("Scenario #%d:\n",t);
printf("%d\n\n",dp[n-1][m-1]);
}
return 0;
}
HDU 2391 Filthy Rich (dp)的更多相关文章
- hdu 2391 Filthy Rich
单纯dp 水一 处理时间点,第一行和第一列特殊处理: 其余的w[i][j]=show(w[i-1][j-1],w[i-1][j],w[i][j-1]); <span style="fo ...
- hdu_2391 Filthy Rich DP
Filthy Rich Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tota ...
- HDU 1011 树形背包(DP) Starship Troopers
题目链接: HDU 1011 树形背包(DP) Starship Troopers 题意: 地图中有一些房间, 每个房间有一定的bugs和得到brains的可能性值, 一个人带领m支军队从入口(房 ...
- hdu 2296 aC自动机+dp(得到价值最大的字符串)
Ring Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- HDU 4778 状压DP
一看就是状压,由于是类似博弈的游戏.游戏里的两人都是绝对聪明,那么先手的选择是能够确定最终局面的. 实际上是枚举最终局面情况,0代表是被Bob拿走的,1为Alice拿走的,当时Alice拿走且满足变换 ...
- HDOJ(HDU).3466 Dividing coins ( DP 01背包 无后效性的理解)
HDOJ(HDU).3466 Dividing coins ( DP 01背包 无后效性的理解) 题意分析 要先排序,在做01背包,否则不满足无后效性,为什么呢? 等我理解了再补上. 代码总览 #in ...
- HDOJ(HDU).2546 饭卡(DP 01背包)
HDOJ(HDU).2546 饭卡(DP 01背包) 题意分析 首先要对钱数小于5的时候特别处理,直接输出0.若钱数大于5,所有菜按价格排序,背包容量为钱数-5,对除去价格最贵的所有菜做01背包.因为 ...
- HDOJ(HDU).2602 Bone Collector (DP 01背包)
HDOJ(HDU).2602 Bone Collector (DP 01背包) 题意分析 01背包的裸题 #include <iostream> #include <cstdio&g ...
- HDOJ(HDU).1058 Humble Numbers (DP)
HDOJ(HDU).1058 Humble Numbers (DP) 点我挑战题目 题意分析 水 代码总览 /* Title:HDOJ.1058 Author:pengwill Date:2017-2 ...
随机推荐
- python 9*9乘法口诀表
# -*- coding: utf-8 -*- # __author__ = 'Carry' for i in range(1, 10): for j in range(1, i + 1): prin ...
- UVA11624_Fire!
在一个矩形方阵里面,一个人要从一个位置走向另一个位置,其中某些地方有火源,每过一分钟,火源就会点燃相邻的点,同时相邻的点也变成了火源.人不能通过有火的点.问一个人能够安全地走到目的地去?最短时间多少? ...
- Hihocoder之conv2d()
http://hihocoder.com/contest/tupu2018/problem/2 题目2 : Standard 2D Convolution 时间限制:5000ms 单点时限:1000 ...
- Java多线程相关的
很多小伙伴在学习Java的时候,总是感觉Java多线程在实际的业务中很少使用,以至于不会花太多的时间去学习,技术债不断累积!等到了一定程度的时候对于与Java多线程相关的东西就很难理解,今天需要探讨的 ...
- 51nod 1483 化学变换 | 二进制 暴力
51nod 1483 化学变换 题面 给出n个整数(n <= 1e5,每个数 <= 1e5),对每个数都可以进行多次操作,操作有两种:乘二/整除以二. 问最少经过多少次操作能使所有数相等. ...
- UVA.12716 GCD XOR (暴力枚举 数论GCD)
UVA.12716 GCD XOR (暴力枚举 数论GCD) 题意分析 题意比较简单,求[1,n]范围内的整数队a,b(a<=b)的个数,使得 gcd(a,b) = a XOR b. 前置技能 ...
- 单点登录(十一)-----遇到问题-----cas启用mongodb验证方式报错--Unable to locate Spring NamespaceHandler for XML schema na
cas启用mongodb验证方式报错--Unable to locate Spring NamespaceHandler for XML schema namespace [http://www.sp ...
- 解题:PA 2014 Bohater
题面 我们把怪分成两类,打完了了能回血的和打完了不能回血的,然后分开打. 对于能回血的,我们先打攻击力低的,因为如果先打一个攻击力高的显然不一定能直接打过,所以先打一些攻击力低的回回血. 对于不能回血 ...
- bzoj2758【scoi2012】Blinker的的噩梦
题目描述 一天Blinker醒来,发现自己成为了一个二维世界的点,而且被标记上了一个奇怪的值. 这个世界是由N个边界互不相交(且不相切)的图形组成,这里图形仅包括圆和凸多边形.每个图形还有一个权值.每 ...
- NOIP2015D2总结
今天居然考了一套题.NOIP2015D2. 这是当年的战绩: 360的一等奖线.好强啊! 之前做过2015的D1,但我确实不会做landlord……今天曾祥瑞学长和林可学姐都来了,他们说,朱昶宇AK, ...