SPOJ - AMR11A(dp)

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

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),但是他只能往下走或者往右走,而且经过各一个点的能量都要大于0,如果是正数就会得到这个能量,负数就会失去这个能量,问从(1,1)开始最小要具备多少能量才不会死。(1,1)和(r,c)一定是0.

 

思路:开始想用搜索的,发现r,c的边界是500,肯定超时。就发现要用dp,还是dp大法好。怎么dp呢?顺序dp就有点难度了,因为要记录当前的能量值。而倒序就是一个很好的选择,知道比较从下,从右哪个需要当前更小的能量值。如果当前的能量都大于从下,从右所需,则是1了,因为从开始最小值是1。

代码:

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;
int dp[505][505];
int map[505][505];
int r,c;
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        cin>>r>>c;
        for(int i=1;i<=r;i++)
            for(int j=1;j<=c;j++)
                cin>>map[i][j];
        dp[r][c]=1;//初始化终点的能量值,最小为1 
        for(int i=r-1;i>=1;i--)
            dp[i][c]=max(1,dp[i+1][c]-map[i][c]);//最后一列一定是从往下得到的dp值 
        for(int i=c-1;i>=1;i--)
            dp[r][i]=max(1,dp[r][i+1]-map[r][i]);//最后一行一定是从往右得到的dp值 
        for(int i=r-1;i>=1;i--)
            for(int j=c-1;j>=1;j--)
            {
                dp[i][j]=max(1,min(dp[i+1][j],dp[i][j+1])-map[i][j]);//比较是从下,还是从右所需要的能量小。 
            }
        cout<<dp[1][1]<<endl;
    }
    return 0;
} 

猜你喜欢

转载自www.cnblogs.com/xiongtao/p/9327308.html
今日推荐