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]);下面看代码:
1 #include<iostream> 2 #include<cstdlib> 3 #include<cstring> 4 #include<cstdio> 5 #include<string> 6 #include<cmath> 7 #include<algorithm> 8 #include<stack> 9 #include<queue> 10 #define ll long long 11 #define inf 0x3f3f3f3f 12 #define pi 3.141592653589793238462643383279 13 using namespace std; 14 int t,n,m,map[505][505],dp[505][505]; 15 int main() 16 { 17 cin>>t; 18 while(t--) 19 { 20 cin>>n>>m; 21 for(int i=1; i<=n; ++i) 22 for(int j=1; j<=m;++j) 23 scanf("%d",&map[i][j]); 24 25 dp[n][m] = 1; 26 for(int i=n-1; i>=1; --i) 27 dp[i][m] = max(1,dp[i+1][m] - map[i][m]); //逆推最后一列的dp值 28 29 for(int i=m-1; i>=1; --i) 30 dp[n][i] = max(1,dp[n][i+1] - map[n][i]); //逆推最后一行的dp值 31 32 for(int i=n-1; i>=1; --i) 33 for(int j=m-1; j>=1; --j) 34 dp[i][j] = max(1,min(dp[i+1][j], dp[i][j+1]) - map[i][j]); //逆推剩余点的dp值 35 //只能先推最后一行和最后一列,因为dp值的推导必须是连续的,你当前的dp值必须由已经知晓的dp值推出 36 cout<<dp[1][1]<<endl; 37 } 38 return 0; 39 }