51nod 矩阵取数问题
一个N*N矩阵中有不同的正整数,经过这个格子,就能获得相应价值的奖励,从左上走到右下,只能向下向右走,求能够获得的最大价值。
f[i][j] = max(f[i-1][j], f[i][j-1]) + a[i][j];
边界: f[i][0] = 0; f[0][i] = 0;
#include<cstdio>
#include<algorithm>
#define REP(i, a, b) for(int i = (a); i < (b); i++)
using namespace std;
typedef long long ll;
const int MAXN = 512;
int a[MAXN][MAXN], n;
ll f[MAXN][MAXN];
int main()
{
scanf("%d", &n);
REP(i, 1, n + 1)
REP(j, 1, n + 1)
scanf("%d", &a[i][j]);
REP(i, 1, n + 1)
REP(j, 1, n + 1)
f[i][j] = max(f[i-1][j], f[i][j-1]) + a[i][j];
printf("%lld\n", f[n][n]);
return 0;
}