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.

题意:给你一个r*c的网格从(1,1)点出发,只能向右或者向下走,问你走到(r,c)点所需的最小初始能量,每经过一个格子当前的能量就就加上它的值,当前能量值不能小于等于零。

思路:因为我们要求最小的初始能量,如果正向dp时就要考虑当前积累能量的情况,所以不行,我们就逆向dp,因为它不用考虑路线能量的积累(后面的积累不能被前面用)。

#include<stdio.h>
#include<string.h>
#define INF 0x3fffffff
int dp[510][510];
int mp[510][510];
int max(int a,int b){
if(a>b)
return a;
return b;
}
int min(int a,int b){
if(a<b)
return a;
return b;
}
int main(){
int t;
int r,c,i,j;
scanf("%d",&t);
while(t--){
scanf("%d%d",&r,&c);
memset(dp,0,sizeof(dp));
for(i=0;i<=r+1;i++)
dp[i][c+1]=INF;
for(i=0;i<=c+1;i++)
dp[r+1][i]=INF;
dp[r+1][c]=0;
dp[r][c+1]=0;
for(i=1;i<=r;i++){
for(j=1;j<=c;j++)
scanf("%d",&mp[i][j]);
}
for(i=r;i>0;i--){
for(j=c;j>0;j--){
dp[i][j]=max(1,min(dp[i+1][j],dp[i][j+1])-mp[i][j]);
//printf("%d %d %d\n",i,j,dp[i][j]);
}
}
printf("%d\n",dp[1][1]);
}
return 0;
}

  

AMR11A - Magic Grid的更多相关文章

  1. Spring-2-A Magic Grid(SPOJ AMR11A)解题报告及测试数据

    Magic Grid Time Limit:336MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Description Tha ...

  2. C. Magic Grid 构造矩阵

    C. Magic Grid time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  3. Magic Grid ComboBox JQuery 版

    在MagicCombo组件中嵌入Grid,以支持分页查找和跨页选取 ​ 1. ​2. [代码][JavaScript]单选示例代码     <script type="text/jav ...

  4. CF-1208 C.Magic Grid

    题目 大意:构造一个n行n列的矩阵,使得每一行,每一列的异或和都相等,n是4的倍数. 先看4*4的矩阵,我们很容易构造出符合要求的矩阵,比如 0    1    2    3 4    5    6  ...

  5. 1208C Magic Grid

    题目大意 给你一个n 让你用0~n^2-1的数填满一个n*n的正方形 满足每个数值出现一次且每行每列的异或值相等 输出任意一种方案 分析 我们发现对于4*4的正方形 0  1  2  3 4  5  ...

  6. Codeforces Round #369 (Div. 2) B. Chris and Magic Square 水题

    B. Chris and Magic Square 题目连接: http://www.codeforces.com/contest/711/problem/B Description ZS the C ...

  7. Chris and Magic Square CodeForces - 711B

    ZS the Coder and Chris the Baboon arrived at the entrance of Udayland. There is a n × n magic grid o ...

  8. SPOJ - AMR11A(DP)

    Thanks a lot for helping Harry Potter in finding the Sorcerer's Stone of Immortality in October. Did ...

  9. B. Chris and Magic Square

    time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...

随机推荐

  1. hdu-1115 计算几何 求重心 凸多边形 面积

    思想是分割成三角形,然后求三角形的重心.那么多边形重心就是若干个三角形的重心带权求中心,可以用质点质心公式. #include <cstdio> #include <iostream ...

  2. apiCloud 调微信支付,调支付宝支付

    data里面的参数信息,需要从后台接口中调取,点击查看微信支付详情,https://docs.apicloud.com/Client-API/Open-SDK/wxPay 首先,需要在config.x ...

  3. BottomNavigationBar使用详解

    gitHub地址:https://github.com/Ashok-Varma/BottomNavigation 一.基本使用 1.在AndroidStudio下添加依赖: compile 'com. ...

  4. spring boot(十二)打包部署

    有很多网友会时不时的问我,spring boot项目如何测试,如何部署,在生产中有什么好的部署方案吗?这篇文章就来介绍一下spring boot 如何开发.调试.打包到最后的投产上线. 开发阶段 单元 ...

  5. mysql 全文搜索(转载http://blog.csdn.net/manbujingxin/article/details/6656992)

    前提:mysql只支持英文内容的全文索引,所以只考虑英文的全文搜索.假定数据表名为post,有三列:id.title.content.id是自增长序号,title是varchar,content是te ...

  6. Oracle 常用sql整理

    1. 查看当前正在只用的undo段 select s.sid, s.serial#, s.username, r.name, t.STATUS, t.START_TIME, t.USED_UBLK, ...

  7. Oracle date timestamp 毫秒 - 时间函数总结(转)

    原文地址:Oracle date timestamp 毫秒 - 时间函数总结 yyyy-mm-dd hh24:mi:ss.ff  年-月-日 时:分:秒.毫秒 --上一月,上一年select add_ ...

  8. div中文字上下居中

    <div class="title">Title</div> 1. 将div高度设成定值 2. 将line-height设成定值 3. 将text-alig ...

  9. CentOS查看安装包会释放哪些文件

    1.查看软件包全称(以mysql为例) rpm -qa | grep -i mysql 2.查看释放出的文件(以MySQL-server-5.5.55-1.el6.x86_64为例) rpm -ql ...

  10. Win32汇编环境搭建教程(MASM32 SDK)

    一.说明 常用的32位汇编编译器有微软的MASM.Borland的TASM和NASM. 编译器 开发者 优点 缺点 MASM 微软 微软自家软件和系统兼容性好:支持invoke/.if等伪指令将汇编变 ...