SPOJ - AMR11A

Posted shuizhidao

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SPOJ - AMR11A相关的知识,希望对你有一定的参考价值。

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 #include <iostream>
 2 #include <cstring>
 3 #include <cmath>
 4 #include <cstdio>
 5 #include <iomanip>
 6 #include <string>
 7 #include <algorithm>
 8 #include <sstream>
 9 #include <vector>
10 #include <set>
11 #include <map>
12 #include <queue>
13 
14 using namespace std;
15 
16 typedef long long LL;
17 const int INF = 0x3f3f3f3f;
18 const int MAXN = 100005;
19 const int MOD = 1e9+7;
20 
21 #define Mem0(x) memset(x, 0, sizeof(x))
22 #define MemM(x) memset(x, 0x3f, sizeof(x))
23 
24 //dp,从 i,j点到终点的最小需求分数
25 int a[505][505], dp[505][505];
26 int main()
27 {
28     int T;
29     cin >> T;
30     while(T--)
31     {
32         Mem0(a);
33         Mem0(dp);
34         int n, m;
35         cin >> n >> m;
36         int i, j;
37         for(i = 0;i < n;++i)
38             for(j = 0;j < m;++j)
39             cin >> a[i][j];
40 
41         dp[n - 1][m - 1] = 1;
42         for(i = n - 2;i >= 0;--i)
43             dp[i][m - 1] = max(dp[i + 1][m - 1] - a[i][m - 1], 1);
44         for(i = m - 2;i >= 0;--i)
45             dp[n - 1][i] = max(dp[n - 1][i + 1] - a[n - 1][i], 1);
46         for(i = n - 2;i >= 0;--i)
47             for(j = m - 2;j >= 0;--j)
48             dp[i][j] = max(min(dp[i + 1][j], dp[i][j + 1]) - a[i][j], 1);
49         cout << dp[0][0] << endl;
50     }
51     return 0;
52 }

 















以上是关于SPOJ - AMR11A的主要内容,如果未能解决你的问题,请参考以下文章

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

SPOJ - AMR11H

SPOJ - AMR11H Array Diversity (水题排列组合或容斥)

SPOJ Distinct Primes 打表

AMR 文件的 FFT 计算中的问题

融云 AMR(Aduio) 播放 AMR 格式 Base64 码音频