hdu2391(简单dp)

来源:互联网 发布:网络语言爸爸什么意思 编辑:程序博客网 时间:2024/04/20 14:03

Filthy Rich

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1875    Accepted Submission(s): 849

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2391
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
13 41 10 8 80 0 1 80 27 0 4
 

Sample Output
Scenario #1:42
解题思路:
题目大意就是给你一个n * m的矩阵,问你每次只能走右、下、右下这三个方向,从坐上出发到达右下,每个格子有相应的金币数,问最多能得到多少金币。
根据dp的思想,对任意一个状态,我可以推导它的前一个状态。对于本题来说,我们还可以进一步优化,因为规定每个格子的金币数为正数,所以当你选择直接走右下这个策略并不是最优的,我们不妨走两个格子到达右下,总比一个格子要好得多(没准多走的那个格子有金币呢)。
状态转移方程dp[i][j] = max(dp[i-1][j] , dp[i][j-1]);
最后注意每组样例结束后都要有一个空行,不然报PE。
完整代码:
#include <functional>#include <algorithm>#include <iostream>#include <fstream>#include <sstream>#include <iomanip>#include <numeric>#include <cstring>#include <climits>#include <cassert>#include <complex>#include <cstdio>#include <string>#include <vector>#include <bitset>#include <queue>#include <stack>#include <cmath>#include <ctime>#include <list>#include <set>#include <map>using namespace std;#pragma comment(linker, "/STACK:102400000,102400000")typedef long long LL;typedef double DB;typedef unsigned uint;typedef unsigned long long uLL;/** Constant List .. **/ //{const int MOD = int(1e9)+7;const int INF = 0x3f3f3f3f;const LL INFF = 0x3f3f3f3f3f3f3f3fLL;const DB EPS = 1e-9;const DB OO = 1e20;const DB PI = acos(-1.0); //M_PI;int maps[1111][1111];int dp[1111][1111];int n , m;int main(){    #ifdef DoubleQ    freopen("in.txt","r",stdin);    #endif    int T;    scanf("%d",&T);    int cas = 1;    while(T--)    {        scanf("%d%d",&n,&m);        for(int i = 0 ; i < n ; i ++)        {            for(int j = 0 ; j < m ; j ++)                scanf("%d",&maps[i][j]);        }        int ans = 0;        for(int i = 0 ; i < n ; i ++)        {            for(int j = 0 ; j < m ; j ++)            {                maps[i][j] += max(maps[i-1][j],maps[i][j-1]);            }        }        printf("Scenario #%d:\n%d\n\n",cas++ , maps[n-1][m-1]);    }}



0 0
原创粉丝点击