题目连接

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. 解决Linux关闭SSH,终端后运行程序终止问题(包括后台)

    问题描述: 每次SSH到服务器上,然后运行了一个自己写的服务端程序,比如 ./myserver.sh ,然后关闭ssh或者终端之后,发现服务不能访问. 简要分析下: 根据   这篇博文  的提示,ss ...

  2. dividend = Integer.parseInt(args[0])参数问题

    先来一段代码: package yichang; public class MyExceptionTest { public static void main(String[] args) { int ...

  3. poj 2299 Ultra-QuickSort(树状数组求逆序数)

    链接:http://poj.org/problem?id=2299 题意:给出n个数,求将这n个数从小到大排序,求使用快排的需要交换的次数. 分析:由快排的性质很容易发现,只需要求每个数的逆序数累加起 ...

  4. Redis(一) 安装

    选择在Linux下安装redis,现在采用虚拟机安装的centos7 进行安装的 1.安装gcc yum install gcc-c++ 2.下载redis安装包,在root目录下执行 wget ht ...

  5. ie6 ie7 userdata 本地存储 引发的惨案.

    我使用 documentElement 作为userdata 作为本地存储的载体. document.documentElement.addBehavior("#default#userda ...

  6. avalon学习教程

    最近在项目中发现了个很不错的前端MVVM框架 avalon,对于基础的使用大概学习了一遍,有些深入的没应用场景还没细看. 收藏好,估计以后要用 http://www.html-js.com/artic ...

  7. js小功能记录

    个人日常中遇到的js小功能记录,方便查看. /** * 判断是否包含字符串某字符串 * @param {[type]} str [被检测的字符串] * @param {[type]} substr [ ...

  8. js仿QQ拖拽删除

    原生js实现仿QQ拖拽删除交互,无需任何依赖. 项目演示请看这里, gitHub请移步这里. 由于源码很长,所以贴到最下面了. 效果截图如下: 核心思想呢,就是点击圆点的时候全屏覆盖个canvas,在 ...

  9. mapreduce方式操作hbase

    一.导入数据到hbase 1.配置hbase-site.xml指向hdfs <configuration> <property> <name>hbase.rootd ...

  10. BZOJ 2745: [HEOI2012]Bridge

    2745: [HEOI2012]Bridge Time Limit: 30 Sec  Memory Limit: 128 MBSubmit: 199  Solved: 90[Submit][Statu ...