A.
关于DP,值得一学,好好思考一下递推过程
加油!
题目大意:从(0,0)走到(n,m)只能向右或者向下,初始有能量1,到达某一点要保证加完权值之后大于0,求还需添加最小能量为多少
//Magic Grid
分析:可以用光搜,也可以DP
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
const int maxn = 500+10;
const int inf = 99999;
int T, r, c, ans;
int f[maxn][maxn], s[maxn][maxn];
int dp(int x, int y)
{
if(x > r || y > c) return inf;
if(f[x][y] != inf) return f[x][y];
return f[x][y] = min(max(dp(x+1, y)-s[x][y], 1), max(dp(x, y+1)-s[x][y], 1));
}
int main()
{
cin >> T;
while(T--) {
cin >> r >> c;
for(int i = 1; i <= r; i++)
for(int j = 1; j <= c; j++)
f[i][j] = inf;
f[r][c] = 1;
for(int i = 1; i <= r; i++)
for(int j = 1; j <= c; j++)
scanf("%d", s[i][j]);
cout << dp(1, 1) << endl;
}
return 0;
}