题目大意:
从(0,0)点开始走,每次垂直或水平行走K步,下一步所获得的价值必须比当前步的价值要大,问所能获得的最大价值。
思路:
DFS+DP
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
/*DFS + DP */
int a[105][105], dp[105][105];
int k, n;
int dfs(int i, int j)
{
if (dp[i][j] > 0)
return dp[i][j];
int p1, p2, maxx = 0;
p1 = i - k;
p2 = i + k;
if (p1 < 0)
p1 = 0;
if (p2 >= n)
p2 = n-1;
for (int l = p1; l <= p2; ++l)
{
if (a[i][j] < a[l][j])
{
int t = dfs(l,j);
maxx = max(t,maxx);
}
}
p1 = j - k;
p2 = j + k;
if (p1 < 0)
p1 = 0;
if (p2 >= n)
p2 = n-1;
for (int l = p1; l <= p2; ++l)
{
if (a[i][j] < a[i][l])
{
int t = dfs(i,l);
maxx = max(t,maxx);
}
}
dp[i][j] = a[i][j] + maxx;
return dp[i][j];
}
int main()
{
while (cin >> n >> k && n > 0)
{
for (int i = 0; i != n; ++i)
for (int j = 0; j != n; ++j)
{
scanf("%d",&a[i][j]);
dp[i][j] = 0;
}
cout << dfs(0,0) << endl;
}
return 0;
}