SPOJ - AMR11A(DP)
Thanks a lot for helping Harry Potter in finding the Sorcerer's Stone of Immortality in October. Did we not tell you that it was just an online game ? uhhh! now here is the real onsite task for Harry. You are given a magrid S ( a magic grid ) having R rows and C columns. Each cell in this magrid has either a Hungarian horntail dragon that our intrepid hero has to defeat, or a flask of magic potion that his teacher Snape has left for him. A dragon at a cell (i,j) takes away |S[i][j]| strength points from him, and a potion at a cell (i,j) increases Harry's strength by S[i][j]. If his strength drops to 0 or less at any point during his journey, Harry dies, and no magical stone can revive him.
Harry starts from the top-left corner cell (1,1) and the Sorcerer's Stone is in the bottom-right corner cell (R,C). From a cell (i,j), Harry can only move either one cell down or right i.e., to cell (i+1,j) or cell (i,j+1) and he can not move outside the magrid. Harry has used magic before starting his journey to determine which cell contains what, but lacks the basic simple mathematical skill to determine what minimum strength he needs to start with to collect the Sorcerer's Stone. Please help him once again.
Input (STDIN):
The first line contains the number of test cases T. T cases follow. Each test case consists of R C in the first line followed by the description of the grid in R lines, each containing C integers. Rows are numbered 1 to R from top to bottom and columns are numbered 1 to C from left to right. Cells with S[i][j] < 0 contain dragons, others contain magic potions.
Output (STDOUT):
Output T lines, one for each case containing the minimum strength Harry should start with from the cell (1,1) to have a positive strength through out his journey to the cell (R,C).
Constraints:
1 ≤ T ≤ 5
2 ≤ R, C ≤ 500
-10^3 ≤ S[i][j] ≤ 10^3
S[1][1] = S[R][C] = 0
Sample Input:
3
2 3
0 1 -3
1 -2 0
2 2
0 1
2 0
3 4
0 -2 -3 1
-1 4 0 -2
1 -2 -3 0
Sample Output:
2
1
2
Explanation:
Case 1 : If Harry starts with strength = 1 at cell (1,1), he cannot maintain a positive strength in any possible path. He needs at least strength = 2 initially.
Case 2 : Note that to start from (1,1) he needs at least strength = 1.
题意:给出了你一个n*m的矩阵,矩阵的元素有正有负,第一个元素和最后一个元素均为0,你从第一个元素开始,每次只能从当前位置向下或向右走一格,走到哪一个元素,你的点数就加上这个元素,在走的途中,你的点数不能小于等于0,问你走到右下角最后一个元素,最少需要初始点数多少,能够让你的点数一直大于0?
思路:一开始想的都是用DFS搜索最佳路径,但是n和m是1-500,限制时间336ms,肯定超时,所以这道题可以用DP来写。声明一个数组dp【i】【j】,它表示你走到第i行第j列时至少还要剩下多少点数。然后每个位置的dp值都必须大于等于1,因为点数为0就代表游戏结束了。dp数组要从最后一个元素往前推,因为你当前要剩多少点数,是由你后面要花多少点数来决定的,所以初始化最后一个元素dp【n】【m】为1。先推最后一行和最后一列,因为如果你走到了最后一列,那接下来你只能往下走了,而走到了最后一行,你只能往右走。最后一行的递推公式是dp[i][m] = max(1,dp[i+1][m] - map[i][m]),它表示你走到下一步需要剩下dp【i+1】【m】的点数,那你现在要剩下的点数就是你下一步需要的点数加上你这一步的消耗,或者是减去你这一步可以得到的点数;但是,你剩下的点数不能小于1,所以就有了上面的公式。最后一列的公式相似。而重点的1-(n-1)行的公式有些不同,这里的点可以走两条路,下或右,选择哪一条呢?当然是可以不用剩太多点数的那一条,因为此题的目的就是怎样让所需点数最少,所以被减数越小,结果越小,就有了公式dp[i][j] = max(1,min(dp[i+1][j], dp[i][j+1]) - map[i][j]);下面看代码:
- #include<iostream>
- #include<cstdlib>
- #include<cstring>
- #include<cstdio>
- #include<string>
- #include<cmath>
- #include<algorithm>
- #include<stack>
- #include<queue>
- #define ll long long
- #define inf 0x3f3f3f3f
- #define pi 3.141592653589793238462643383279
- using namespace std;
- int t,n,m,map[][],dp[][];
- int main()
- {
- cin>>t;
- while(t--)
- {
- cin>>n>>m;
- for(int i=; i<=n; ++i)
- for(int j=; j<=m;++j)
- scanf("%d",&map[i][j]);
- dp[n][m] = ;
- for(int i=n-; i>=; --i)
- dp[i][m] = max(,dp[i+][m] - map[i][m]); //逆推最后一列的dp值
- for(int i=m-; i>=; --i)
- dp[n][i] = max(,dp[n][i+] - map[n][i]); //逆推最后一行的dp值
- for(int i=n-; i>=; --i)
- for(int j=m-; j>=; --j)
- dp[i][j] = max(,min(dp[i+][j], dp[i][j+]) - map[i][j]); //逆推剩余点的dp值
- //只能先推最后一行和最后一列,因为dp值的推导必须是连续的,你当前的dp值必须由已经知晓的dp值推出
- cout<<dp[][]<<endl;
- }
- return ;
- }
SPOJ - AMR11A(DP)的更多相关文章
- LightOJ 1033 Generating Palindromes(dp)
LightOJ 1033 Generating Palindromes(dp) 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid= ...
- lightOJ 1047 Neighbor House (DP)
lightOJ 1047 Neighbor House (DP) 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=87730# ...
- UVA11125 - Arrange Some Marbles(dp)
UVA11125 - Arrange Some Marbles(dp) option=com_onlinejudge&Itemid=8&category=24&page=sho ...
- 【POJ 3071】 Football(DP)
[POJ 3071] Football(DP) Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4350 Accepted ...
- 初探动态规划(DP)
学习qzz的命名,来写一篇关于动态规划(dp)的入门博客. 动态规划应该算是一个入门oier的坑,动态规划的抽象即神奇之处,让很多萌新 萌比. 写这篇博客的目标,就是想要用一些容易理解的方式,讲解入门 ...
- Tour(dp)
Tour(dp) 给定平面上n(n<=1000)个点的坐标(按照x递增的顺序),各点x坐标不同,且均为正整数.请设计一条路线,从最左边的点出发,走到最右边的点后再返回,要求除了最左点和最右点之外 ...
- 2017百度之星资格赛 1003:度度熊与邪恶大魔王(DP)
.navbar-nav > li.active > a { background-image: none; background-color: #058; } .navbar-invers ...
- Leetcode之动态规划(DP)专题-详解983. 最低票价(Minimum Cost For Tickets)
Leetcode之动态规划(DP)专题-983. 最低票价(Minimum Cost For Tickets) 在一个火车旅行很受欢迎的国度,你提前一年计划了一些火车旅行.在接下来的一年里,你要旅行的 ...
- 最长公共子序列长度(dp)
/// 求两个字符串的最大公共子序列长度,最长公共子序列则并不要求连续,但要求前后顺序(dp) #include <bits/stdc++.h> using namespace std; ...
随机推荐
- IDA Pro 权威指南学习笔记(八) - 基本 IDA 导航
导航目标 在分析阶段,IDA 会通过检查二进制文件的符号表生成符号名称,或根据二进制文件引用位置的方式自动生成一个名称 反汇编窗口中显示的任何名称都是导航目标 双击任何一个符号,IDA 将跳转到相应的 ...
- JS 对输入框文本正在输入中校验
// keyup getInput.keyup(function(){ var a = parseInt(getPrice); var b = parseInt(getInput.val()); // ...
- 核主成分分析(Kernel Principal Component Analysis, KPCA)的公式推导过程
KPCA,中文名称”核主成分分析“,是对PCA算法的非线性扩展,言外之意,PCA是线性的,其对于非线性数据往往显得无能为力,例如,不同人之间的人脸图像,肯定存在非线性关系,自己做的基于ORL数据集的实 ...
- RabbitMQ_direct
#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ @version: @author: morgana @licens ...
- 基于Tesseract的身份证识别Android端应用
以开源的Tesseract为基础,做了一个身份证识别的app. 图片资源是百度找的,而且手机对着电脑屏幕拍照,拍出很多花纹,影响比较大,所以误差不小,实测对着自己身份证拍照会好很多. 效果图: 1.拍 ...
- Linux实战教学笔记45:NoSQL数据库之redis持久化存储(一)
第1章 redis存储系统 1.1 redis概述 REmote DIctionary Server(Redis)是一个基于key-value键值对的持久化数据库存储系统.redis和大名鼎鼎的Mem ...
- strncmp memcmp区别
内部实现:前者逐每个字符进行比较,并判当前字符是否为0: 后者逐内存块进行比较. 效率:后者自然要优,不论从内部实现上,还是系统优化上. 场景:后者无法替代前者.在项目中遇到一种情况,两个字符串比较, ...
- 关于dojo自定义类
dojo自定义类时,只要没有在constructor函数中传参改变的变量,都属于静态变量,因此不能用this.访问,而是直接用变量名访问
- Python将阿拉伯数字转化为中文大写-乾颐堂
利用Python将阿拉伯数字转化为中文大写,其实最麻烦的地方就是中间空多个0的问题,这种情况下,采用拆分法则,将一个大数字,先拆分成整数部分和小数部分,再对整数部分按照仟.万.亿.兆分位拆分为四个字符 ...
- elasticsearch-jdbc 使用
elasticsearch-jdbc是一个将关系型数据库(RDBMS)数据导入到ElasticSearch库中的一个工具包,支持mysql.oracle.postgrey.csv等存储列式数据的容器. ...