标签:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
#define met(a,b) (memset(a,b,sizeof(a)))
#define N 110
#define INF 0xffffff
int a[N][N], dp[N][N];
int dir[4][2]={{-1,0},{1,0},{0,-1},{0,1}};
int DFS(int n, int k, int x, int y)
{
if(!dp[x][y])
{
int ans=0;
for(int i=1; i<=k; i++)
{
int temp=0;
for(int j=0; j<4; j++)
{
int nx = x + dir[j][0]*i;
int ny = y + dir[j][1]*i;
if(nx>=1 && nx<=n && ny>=1 && ny<=n && a[nx][ny]>a[x][y])
{
temp = max(temp, DFS(n, k, nx, ny));
}
}
ans = max(ans, temp);
}
dp[x][y] = ans + a[x][y];
}
return dp[x][y];
}
int main()
{
int n, k;
while(scanf("%d%d", &n, &k), n!=-1||k!=-1)
{
int i, j;
met(a, 0);
met(dp, 0);
for(i=1; i<=n; i++)
for(j=1; j<=n; j++)
scanf("%d", &a[i][j]);
printf("%d\n", DFS(n, k, 1, 1));
}
return 0;
}
http://acm.hdu.edu.cn/showproblem.php?pid=1078
(记忆化搜索) FatMouse and Cheese(hdu 1078)
标签:
原文地址:http://www.cnblogs.com/YY56/p/5448139.html