TZOJ 1800 Martian Mining(二维dp)
描述
The NASA Space Center, Houston, is less than 200 miles from San Antonio, Texas (the site of the ACM Finals this year). This is the place where the astronauts are trained for Mission Seven Dwarfs, the next giant leap in space exploration. The Mars Odyssey program revealed that the surface of Mars is very rich in yeyenum and bloggium. These minerals are important ingredients for certain revolutionary new medicines, but they are extremely rare on Earth. The aim of Mission Seven Dwarfs is to mine these minerals on Mars and bring them back to Earth.
The Mars Odyssey orbiter identified a rectangular area on the surface of
Mars that is rich in minerals. The area is divided into cells that form
a matrix of n rows and m columns, where the rows go from east to west
and the columns go from north to south. The orbiter determined the
amount of yeyenum and bloggium in each cell. The astronauts will build a
yeyenum refinement factory west of the rectangular area and a bloggium
factory to the north. Your task is to design the conveyor belt system
that will allow them to mine the largest amount of minerals.
There are two types of conveyor belts: the first moves minerals from
east to west, the second moves minerals from south to north. In each
cell you can build either type of conveyor belt, but you cannot build
both of them in the same cell. If two conveyor belts of the same type
are next to each other, then they can be connected. For example, the
bloggium mined at a cell can be transported to the bloggium refinement
factory via a series of south-north conveyor belts.
The minerals are very unstable, thus they have to be brought to the
factories on a straight path without any turns. This means that if there
is a south-north conveyor belt in a cell, but the cell north of it
contains an east-west conveyor belt, then any mineral transported on the
south-north conveyor beltwill be lost. The minerals mined in a
particular cell have to be put on a conveyor belt immediately, in the
same cell (thus they cannot start the transportation in an adjacent
cell). Furthermore, any bloggium transported to the yeyenum refinement
factory will be lost, and vice versa.
Your program has to design a conveyor belt system that maximizes the
total amount of minerals mined,i.e., the sum of the amount of yeyenum
transported to the yeyenum refinery and the amount of bloggium
transported to the bloggium refinery.
输入
The
input contains several blocks of test cases. Each case begins with a
line containing two integers: the number 1 ≤ n ≤ 500 of rows, and the
number 1 ≤ m ≤ 500 of columns. The next n lines describe the amount of
yeyenum that can be found in the cells. Each of these n lines contains m
integers. The first line corresponds to the northernmost row; the first
integer of each line corresponds to the westernmost cell of the row.
The integers are between 0 and 1000. The next n lines describe in a
similar fashion theamount of bloggium found in the cells.
The input is terminated by a block with n = m = 0.
输出
For each test case, you have to output a single integer on a separate line: the maximum amount of mineralsthat can be mined.
样例输入
4 4
0 0 10 9
1 3 10 0
4 2 1 3
1 1 20 0
10 0 0 0
1 1 1 30
0 0 5 5
5 10 10 10
0 0
样例输出
98
提示
Huge input file, 'scanf' recommended to avoid TLE.
题意
N*M的方格,每个方格里有A和B矿石,A矿石只能往左运输,B只能往上,每个方格只能放最多1个传送带,问你最大能得到多少矿石
题解
dp[i][j]代表从(1,1)->(n,m)所得到的最大矿石
可以知道dp[i][j]=max(dp[i-1][j]+a[i][j],dp[i][j-1]+b[i][j]);
dp[i-1][j]+a[i][j];代表(1,1)->(i-1,j)的最大值,加上当前点左边的全部左移
dp[i][j-1]+a[i][j];代表(1,1)->(i,j-1)的最大值,加上当前点上边的全部上移
初始值i=0或j=0,dp[i][j]=0
代码
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int N=;
int n,m;
int dp[N][N],a[][N][N];
int main()
{
while(scanf("%d%d",&n,&m)!=EOF,n||m)
{
for(int k=;k<;k++)
for(int i=;i<=n;i++)
for(int j=;j<=m;j++)
{
scanf("%d",&a[k][i][j]);
if(!k)a[][i][j]+=a[][i][j-];
else a[][i][j]+=a[][i-][j];
}
memset(dp,,sizeof dp);
for(int i=;i<=n;i++)
for(int j=;j<=m;j++)
dp[i][j]=max(dp[i-][j]+a[][i][j],dp[i][j-]+a[][i][j]);
printf("%d\n",dp[n][m]);
}
return ;
}
TZOJ 1800 Martian Mining(二维dp)的更多相关文章
- 洛谷p1732 活蹦乱跳的香穗子 二维DP
今天不BB了,直接帖原题吧 地址>>https://www.luogu.org/problem/show?pid=1732<< 题目描述 香穗子在田野上调蘑菇!她跳啊跳,发现 ...
- HDU - 2159 FATE(二维dp之01背包问题)
题目: 思路: 二维dp,完全背包,状态转移方程dp[i][z] = max(dp[i][z], dp[i-1][z-a[j]]+b[j]),dp[i][z]表示在杀i个怪,消耗z个容忍度的情况下 ...
- 传纸条 NOIP2008 洛谷1006 二维dp
二维dp 扯淡 一道比较基本的入门难度的二维dp,类似于那道方格取数,不过走过一次的点下次不能再走(看提交记录里面好像走过一次的加一次a[i][j]的也AC了,,),我记得当年那道方格取数死活听不懂, ...
- 洛谷P1048 采药 二维dp化一维
题目描述 辰辰是个天资聪颖的孩子,他的梦想是成为世界上最伟大的医师.为此,他想拜附近最有威望的医师为师.医师为了判断他的资质,给他出了一个难题.医师把他带到一个到处都是草药的山洞里对他说:“孩子,这个 ...
- 关于二维DP————站上巨人的肩膀
意匠惨淡经营中ing, 语不惊人死不休........ 前几天学了DP,做了个简单的整理,记录了关于DP的一些概念之类的,今天记录一下刚学的一个类型 ----关于二维DP 那建立二维数组主要是干嘛用的 ...
- BZOJ 2748: [HAOI2012]音量调节【二维dp,枚举】
2748: [HAOI2012]音量调节 Time Limit: 3 Sec Memory Limit: 128 MBSubmit: 2010 Solved: 1260[Submit][Statu ...
- To the Max 二维dp(一维的变形)
Description Given a two-dimensional array of positive and negative integers, a sub-rectangle is any ...
- POJ 1661 Help Jimmy(二维DP)
题目链接:http://poj.org/problem?id=1661 题目大意: 如图包括多个长度和高度各不相同的平台.地面是最低的平台,高度为零,长度无限. Jimmy老鼠在时刻0从高于所有平台的 ...
- SGU104 二维dp
大致题意: n个东西放在(1.2.3...m)个容器中,先放的必需在后方的左边.a[i][j]表示i号物品放在j容器所得 的价值,求最大价值. 几乎是刚刚开始接触动态规划题,开始我这样想 每个东西一件 ...
随机推荐
- 【JEECG技术文档】JEECG在线聊天插件功能集成文档
原文地址:http://jeecg.iteye.com/blog/2320670 JEECG在线聊天插件功能集成文档 前提: 采用jeecg_3.6.3版本以上(Maven工程) 插件项目: 在线聊天 ...
- mysql 索引,转载
from:http://blog.csdn.net/zhanglu0223/article/details/8713149 1. 索引建立的原则 用于索引的最好的备选数据列是那些出现在WHERE子句. ...
- js 引入外部文件之 script 标签
在我的理解看来,html 就是一个单纯的管显示问题,js就是单纯的管动作问题,css就是单纯的管布局问题,这三个构成了一个网页 在HTML中,经常会用到引入js 文件. 引入js的方法很简单: 1. ...
- js 选项卡制作
知识回顾,制作JS选项卡,仅供参考 html代码: <!DOCTYPE html> <html lang="en"> <head> <me ...
- 2339 3.1.1 Agri-Net 最短网络
Description 农民约翰被选为他们镇的镇长!他其中一个竞选承诺就是在镇上建立起互联网,并连接到所有的农场.当然,他需要你的帮助. 约翰已经给他的农场安排了一条高速的网络线路,他想把这条线路共享 ...
- (FireDAC) 连接定义
Defining Connection (FireDAC) 连接定义就是应用程序使用特定的FireDAC驱动连接数据库的参数集合.相当于BDE的别名,ADO的UDL,或者ODBC的DSN. For ...
- Eclipse开发Web常见异常
1.java.lang.IllegalStateException: Web app root system property already set to different value 错误原因: ...
- Linux命令:ssh
ssh介绍 ssh用法 ssh帮助 SSH() BSD General Commands Manual SSH() NAME ssh — OpenSSH SSH client (remote logi ...
- A*寻路初探(转载)
启发式搜索:启发式搜索就是在状态空间中的搜索对每一个搜索的位置进行评估,得到最好的位置,再从这个位置进行搜索直到目标.这样可以省略大量无畏的搜索路径,提到了效率.在启发式搜索中,对位置的估价是十分重要 ...
- C# DataTable to Html
原地址:忘了 /// <summary> /// DataTable 转换为 Html /// </summary> /// <param name="dt&q ...