Magic Grid

Time Limit:336MS     Memory Limit:0KB     64bit IO Format:%lld & %llu

Description

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.

题解:

  1. 首先在任何位置剩下的体力至少为1,而且只能向下或者是向右,那么很容易想到动态规划,用m记录当前地图上将消耗或是增加的体力,然后从最后一个位置往前,每个位置计算出当前位置需要的最少的体力。最后计算到第一个位置即可。

  2. 利用递推式  d[i][j]= max(1,min(d[i+1][j]-m[i][j],d[i][j+1]-m[i][j])); 最后面一列和最下面的一行先计算。

以下是代码:

#include <cstdio>
#include <string>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <algorithm>
using namespace std; #define ss(x) scanf("%d",&x)
#define ff(i,s,e) for(int i=s;i<e;i++)
#define fe(i,s,e) for(int i=s;i<=e;i++)
#define print(x) printf("%d\n",x);
#define write() freopen("1.in","r",stdin); const int N =510;
int m[N][N];
int d[N][N];
int r,c;
void input(){
ss(r);ss(c);
fe(i,1,r)
fe(j,1,c)
ss(m[i][j]);
}
void dp(){
d[r][c]=1;
for(int i=r-1;i>=1;i--)//先计算最右边一列
d[i][c] = max(1,d[i+1][c]-m[i][c]);
for(int i=c-1;i>=1;i--)//先计算最下面一行
d[r][i]= max(1,d[r][i+1]-m[r][i]);
for(int i=r-1;i>=1;i--)
for(int j=c-1;j>=1;j--)//遍历计算其他所有的
d[i][j]= max(1,min(d[i+1][j]-m[i][j],d[i][j+1]-m[i][j]));
}
int main(){
//write();
int T;
ss(T);
while(T--){
input();
dp();
print(d[1][1]);//输出第一个即可;
}
}

  

Spring-2-A Magic Grid(SPOJ AMR11A)解题报告及测试数据的更多相关文章

  1. Spring-2-J Goblin Wars(SPOJ AMR11J)解题报告及测试数据

    Goblin Wars Time Limit:432MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Description Th ...

  2. Spring-2-B Save the Students(SPOJ AMR11B)解题报告及测试数据

    Save the Students Time Limit:134MS     Memory Limit:0KB     64bit IO Format:%lld & %llu   Descri ...

  3. Spring-2-H Array Diversity(SPOJ AMR11H)解题报告及测试数据

    Array Diversity Time Limit:404MS     Memory Limit:0KB     64bit IO Format:%lld & %llu   Descript ...

  4. sgu 104 Little shop of flowers 解题报告及测试数据

    104. Little shop of flowers time limit per test: 0.25 sec. memory limit per test: 4096 KB 问题: 你想要将你的 ...

  5. Spring-1-I 233 Matrix(HDU 5015)解题报告及测试数据

    233 Matrix Time Limit:5000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Descript ...

  6. Spring-1-H Number Sequence(HDU 5014)解题报告及测试数据

    Number Sequence Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Pro ...

  7. Spring-1-F Dice(HDU 5012)解题报告及测试数据

    Dice Time Limit:1000MS     Memory Limit:65536KB Description There are 2 special dices on the table. ...

  8. Spring-1-E Game(HDU 5011)解题报告及测试数据

    Game Time Limit:1000MS     Memory Limit:65536KB Description Here is a game for two players. The rule ...

  9. Spring-1-A Post Robot(HDU 5007)解题报告及测试数据

    Post Robot Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K Problem Description ...

随机推荐

  1. 编程之美 set 14 小飞的电梯调度算法

    题目 电梯每次上升只停一次, 求问电梯停在哪一楼能够保证乘坐电梯的所有乘客爬楼层的层数之和最小 思路 假设电梯的层数是 m, 乘客人数是 n 1. 枚举, 时间复杂度是 o(mn) 2. 滚动解法. ...

  2. svn移动目录时如何保留原来的日志

    [问题描述] 想将SVN下的文件夹A移动目录D下,同时保留文件夹A及其下面文件的SVN日志 [原来的做法]         将文件夹A直接拷贝到目录D,然后提交到SVN [原来做法的问题]   日志无 ...

  3. 【BZOJ4819】[Sdoi2017]新生舞会 01分数规划+费用流

    [BZOJ4819][Sdoi2017]新生舞会 Description 学校组织了一次新生舞会,Cathy作为经验丰富的老学姐,负责为同学们安排舞伴.有n个男生和n个女生参加舞会 买一个男生和一个女 ...

  4. 《从零开始学Swift》学习笔记(Day 10)——运算符是“ +、-、*、/ ”吗?

    原创文章,欢迎转载.转载请注明:关东升的博客 运算符是用于执行程序代码运算,会针对一个或一个以上操作数项目来进行运算.例如:2+3,其操作数是2和3,而运算符则是“+”.那么“+.-.*./”是运算符 ...

  5. Angular2+学习第3篇 基本知识-组件

    一.插值表达式 基本用法与ng1一样. 可以使用 Angular 内置的 json 管道,来显示对象信息,管道用来格式化数据 import { Component } from '@angular/c ...

  6. [ Office 365 开发系列 ] 身份认证

    前言 本文完全原创,转载请说明出处,希望对大家有用. 通常我们在开发一个应用时,需要考虑用户身份认证及授权,Office 365使用AAD(Azure Active Directory)作为其认证机构 ...

  7. JAVA基础之sql模糊匹配、外键以及jsp中include的用法

    一.SQL模糊匹配 适用于对字符串进行模糊搜索 格式:   字段名 Like '%关键词%'      %          表示这个位置可有任意个字符(没有也可以) %关键词%  只要包含关键词就算 ...

  8. CentOS7.2安装配置FTP服务器VSFTP

    1,查看系统版本 2,yum安装vsftpd yum -y install vsftpd 3,修改配置文件 vim /etc/vsftpd/vsftpd.conf local_enable=YES w ...

  9. plsql连接其他服务器的oracle

    plsql除了连接本地的oracle还需要连接其他服务器上的oracle时 1.下载安装oracleClient:2.在oracleClient安装目录下:例如:D:/instantclient_11 ...

  10. 遇到的问题mongodb

    1.MongoNetworkError:failed to connect to server? 数据库没有启动,启动mongo数据库就好 2.有些东西真的是要做好记录的,单纯为了自己日后可以查阅比较 ...