题目连接

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)的更多相关文章

  1. 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 ...

  2. hdu_2391 Filthy Rich DP

    Filthy Rich Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

  3. HDU 1011 树形背包(DP) Starship Troopers

    题目链接:  HDU 1011 树形背包(DP) Starship Troopers 题意:  地图中有一些房间, 每个房间有一定的bugs和得到brains的可能性值, 一个人带领m支军队从入口(房 ...

  4. hdu 2296 aC自动机+dp(得到价值最大的字符串)

    Ring Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  5. HDU 4778 状压DP

    一看就是状压,由于是类似博弈的游戏.游戏里的两人都是绝对聪明,那么先手的选择是能够确定最终局面的. 实际上是枚举最终局面情况,0代表是被Bob拿走的,1为Alice拿走的,当时Alice拿走且满足变换 ...

  6. HDOJ(HDU).3466 Dividing coins ( DP 01背包 无后效性的理解)

    HDOJ(HDU).3466 Dividing coins ( DP 01背包 无后效性的理解) 题意分析 要先排序,在做01背包,否则不满足无后效性,为什么呢? 等我理解了再补上. 代码总览 #in ...

  7. HDOJ(HDU).2546 饭卡(DP 01背包)

    HDOJ(HDU).2546 饭卡(DP 01背包) 题意分析 首先要对钱数小于5的时候特别处理,直接输出0.若钱数大于5,所有菜按价格排序,背包容量为钱数-5,对除去价格最贵的所有菜做01背包.因为 ...

  8. HDOJ(HDU).2602 Bone Collector (DP 01背包)

    HDOJ(HDU).2602 Bone Collector (DP 01背包) 题意分析 01背包的裸题 #include <iostream> #include <cstdio&g ...

  9. HDOJ(HDU).1058 Humble Numbers (DP)

    HDOJ(HDU).1058 Humble Numbers (DP) 点我挑战题目 题意分析 水 代码总览 /* Title:HDOJ.1058 Author:pengwill Date:2017-2 ...

随机推荐

  1. [转帖] sqlserver CAL 授权模式下 只能够有20个core的使用问题

    http://www.cnblogs.com/diabloxl/p/3623640.html?utm_source=tuicool&utm_medium=referral 公司这边性能组老师进 ...

  2. es6 let关键字

    1.let关键字 var arr = [ ]; for(var i=0; i<10; i++){ arr [i] = function(){ alert(i) } } arr [8](); // ...

  3. Longest Substring with At Most Two Distinct

    Given a string, find the length of the longest substring T that contains at most 2 distinct characte ...

  4. Tomcat 启动流程

  5. wazuh官方安装指南(中文译版本)

      安装Wazuh服务器 Wazuh服务器可以安装在任何类型的Unix操作系统上.最常见安装在Linux上.如果可以为您的系统提供自动化脚本,则安装过程会更容易,但是,从源码构建和安装也非常简单. 通 ...

  6. 解题:POI 2014 Ant colony

    题面 既然我们只知道最后数量为$k$的蚂蚁会在特殊边上被吃掉,不妨逆着推回去,然后到达每个叶节点的时候就会有一个被吃掉的蚂蚁的区间,然后二分一下就好啦 #include<cstdio> # ...

  7. PACS&DICOM

    What is DICOM, PACS, and Workstation? What is DICOM? We will take them one at a time – So first of a ...

  8. Linux可执行文件后缀问题

    一般来说,可执行文件没有扩展名. Linux不根据扩展名判断文件类型,而是根据文件的内容来判断.所以扩展名的作用是帮助人来识别文件,对于Linux系统本身来说没有什么用处. .sh结尾表示是shell ...

  9. 【题解】新型城市化 HAOI2017 网络流 二分图最大匹配 强连通分量

    Prelude 好,HAOI2017终于会做一道题了! 传送到洛谷:→_→ 传送到LOJ:←_← 本篇博客链接:(●'◡'●) Solution 首先要读懂题. 考场上我是这样想的QAQ. 我们把每个 ...

  10. 项目经验总结-first

    1. org.apache.commons.lang中StringUtils判空使用经验之谈 StringUtils.isEmpty(String str) 判断字符串str是否为空串且是否长度为0, ...