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 ...
随机推荐
- Sql Server外键约束
一.添加约束(级联删除) 1.创建表结构时添加 create table UserDetails(id int identity(1,1) primary key,name varchar(50) n ...
- vue 组件 子向父亲通信用自定义方法用事件监听
<!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>T ...
- Java并发编程中的设计模式解析(二)一个单例的七种写法
Java单例模式是最常见的设计模式之一,广泛应用于各种框架.中间件和应用开发中.单例模式实现起来比较简单,基本是每个Java工程师都能信手拈来的,本文将结合多线程.类的加载等知识,系统地介绍一下单例模 ...
- java 创建过程
- SSL身份认证原理 - 目标: 搞清楚数字证书和数字签名的关系
1 概述 1.1 产生背景 基于万维网的电子商务和网上银行等新兴应用,极大地方便了人们的日常生活,受到人们的青睐.由于这些应用都需要在网络上进行在线交易,它们对网络通信的安全性提出了更高的要求.传 ...
- 【刷题】BZOJ 3529 [Sdoi2014]数表
Description 有一张n×m的数表,其第i行第j列(1<=i<=n,1<=j<=m)的数值为能同时整除i和j的所有自然数之和.给定a,计算数表中不大于a的数之和. In ...
- Linux系统之路Centos7.2——安装QQ 的一些问题(附VMware的安装)
1.首先安装wine 可以通过源码安装,注意在编译的时候加参数,编译64位(如果你的系统是64位哦!) 但是我建议直接rpm安装. 安装网络源: rpm -ivh epel-release-6-8.n ...
- sqlalchemy外键关联
一.创建两张表,并关联外键 导入ForenginKey模块 # -*- coding: UTF-8 -*- from sqlalchemy import create_engine from sqla ...
- day2 程序流程控制
流程控制:1.调用方法.调用方法将导致控制流程离开当前方法,转移到被调用的方法 2.选择.java中有两种做出选择的机制:if/else语句和switch语句.三目运算符可以看作是if/else的一个 ...
- 转:EasyJSWebView
EasyJSWebView 是类似 Android javascriptInterface 的 uiwebview js 调用原生代码框架 示例代码: 先建一个MyJSInterface接口 @in ...