SPOJ - AMR11A-(DP逆向思维)—— 个人觉得好题

原题链接:https://www.spoj.com/problems/AMR11A/en/

VJ暑期训练链接:https://vjudge.net/contest/237052#problem/A

AMR11A - Magic Grid

no tags

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.

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.

题意:求走到右下角最少需要一开始为多少体力,注意:在走的过程中体力为0或者为“负”时死亡。

思维:用逆向DP表示走到该点走到右下角最少需要多少体力!!!

 1 #include<stdio.h>
 2 int DP[505][505];
 3 int min(int a,int b)
 4 {
 5     if(a<b) return a;
 6     return b;
 7 }
 8 int main()
 9 {
10     int T;
11     scanf("%d",&T);
12     while (T--)
13     {
14         int R,C;
15         scanf("%d%d",&R,&C);
16         for (int i=0;i<R;i++)
17         for (int j=0;j<C;j++)
18         scanf("%d",&DP[i][j]);
19         for (int i=C-2;i>=0;i--)//最后一行先直接更新
20         {
21             int t=DP[R-1][i];
22             DP[R-1][i]=DP[R-1][i+1]-DP[R-1][i];
23             if(DP[R-1][i]<=0)//特殊1
24             DP[R-1][i]=1;
25             if (t<0&&DP[R-1][i+1]==0)//右下角
26             DP[R-1][i]++;//一开始特殊2
27         }
28         for (int i=R-2;i>=0;i--)//最后一列先直接更新
29         {
30             int t=DP[i][C-1];
31             DP[i][C-1]=DP[i+1][C-1]-DP[i][C-1];
32             if(DP[i][C-1]<=0)//特殊1
33             DP[i][C-1]=1;
34             if (t<0&&DP[i+1][C-1]==0)//右下角
35             DP[i][C-1]++;//一开始特殊2
36         }
37         for (int i=R-2;i>=0;i--)//依次更新
38         for (int j=C-2;j>=0;j--)
39         {
40             DP[i][j]=min(DP[i][j+1],DP[i+1][j])-DP[i][j];
41             if(DP[i][j]<=0)//特殊1
42             DP[i][j]=1;
43         }
44         printf("%d\n",DP[0][0]);
45     }
46     return 0;
47  } 

原文地址:https://www.cnblogs.com/bendandedaima/p/9288636.html

时间: 2024-11-20 15:14:39

SPOJ - AMR11A-(DP逆向思维)—— 个人觉得好题的相关文章

HDU 4507 吉哥系列故事――恨7不成妻(数位dp&amp;好魔性的一道好题)

题目链接:[kuangbin带你飞]专题十五 数位DP J - 吉哥系列故事――恨7不成妻 题意 Time Limit:500MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Description 单身! 依然单身! 吉哥依然单身! DS级码农吉哥依然单身! 所以,他生平最恨情人节,不管是214还是77,他都讨厌! 吉哥观察了214和77这两个数,发现: 2+1+4=7 7+7=7*2 77=7*11 最终,他发现原来这一切归根到底都是

【DP】一道递推题。。。

Bob想要构造一张由n个节点构成的图.构造的过程由两步组成:首先Bob会取出n个孤立的点,并把它们从1到n编号,然后对每个节点用一种颜色染色,Bob一共可以使用K种不同的颜色.接下来Bob会在这张图中加入一些有向边,对于每一个编号范围在[2,n]的节点i,Bob有可能选择一个节点j满足j < i并且节点i与j的颜色不同,然后加入一条从i到j的有向边,对于节点i,Bob也有可能不加任何的有向边.现在你需要回答Bob有可能构造出多少种不同的图. 输入: 第一行包含三个整数n,K. 输出: 输出一个整

hdu 5074 DP 2014鞍山现场赛题

hdu 5074 http://acm.hdu.edu.cn/showproblem.php?pid=5074 挺水的DP,注意依a[i-1]和a[i]的正负区分状态转移,然后O(n^3)即可轻易解决,我DP挺弱的也能过,貌似也就CF C题水平 //#pragma comment(linker, "/STACK:102400000,102400000") #include <cstdio> #include <cstring> #include <algo

zoj 3822 Domination 概率dp 2014牡丹江站D题

Domination Time Limit: 8 Seconds      Memory Limit: 131072 KB      Special Judge Edward is the headmaster of Marjar University. He is enthusiastic about chess and often plays chess with his friends. What's more, he bought a large decorative chessboar

nyoj 546——Divideing Jewels——————【dp、多重背包板子题】

Divideing Jewels 时间限制:1000 ms  |  内存限制:65535 KB 难度:4 描述 Mary and Rose own a collection of jewells. They want to split the collection among themselves so that both receive an equal share of the jewels. This would be easy if all the jewels had the same

SPOJ PGCD - Primes in GCD Table (好题! 莫比乌斯反演+分块求和优化)

PGCD - Primes in GCD Table Johnny has created a table which encodes the results of some operation -- a function of two arguments. But instead of a boring multiplication table of the sort you learn by heart at prep-school, he has created a GCD (greate

SPOJ DQUERY D-query(莫队基础题)

题目链接:http://www.spoj.com/problems/DQUERY/ 题目: Given a sequence of n numbers a1, a2, ..., an and a number of d-queries. A d-query is a pair (i, j) (1 ≤ i ≤ j ≤ n). For each d-query (i, j), you have to return the number of distinct elements in the subs

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

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!

BZOJ 1415 NOI2005 聪聪和可可 期望DP+记忆化搜索 BZOJ200题达成&amp;&amp;NOI2005全AC达成

题目大意:给定一个无向图,聪聪在起点,可可在终点,每个时刻聪聪会沿最短路走向可可两步(如果有多条最短路走编号最小的点),然后可可会等概率向周围走或不动,求平均多少个时刻后聪聪和可可相遇 今天早上起床发现194了然后就各种刷--当我发现199的时候我决定把第200题交给05年NOI仅剩的一道题--结果尼玛调了能有一个小时--我居然没看到编号最小这个限制0.0 首先我们知道,由于聪聪走两步而可可走一步,所以聪聪一定能在有限的时刻追上可可,而且两人的距离随着时间进行单调递减 于是我们记忆化搜索 首先用

dp之状态压缩水题总结

状态压缩dp 其实就是 将状态表示成二进制,然后用位运算进行求解 poj 3311 Hie with the Pie Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 5080   Accepted: 2706 Description The Pizazz Pizzeria prides itself in delivering pizzas to its customers as fast as possible. Unf