FatMouse has stored some cheese in a city. The city can be considered as a square grid of dimension n: each grid location is labelled (p,q) where 0 <= p < n and 0 <= q < n. At each grid location Fatmouse has hid between 0 and 100 blocks of cheese in a hole. Now he's going to enjoy his favorite food.
FatMouse begins by standing at location (0,0). He eats up the cheese where he stands and then runs either horizontally or vertically to another location. The problem is that there is a super Cat named Top Killer sitting near his hole, so each time he can run at most k locations to get into the hole before being caught by Top Killer. What is worse -- after eating up the cheese at one location, FatMouse gets fatter. So in order to gain enough energy for his next run, he has to run to a location which have more blocks of cheese than those that were at the current hole.
Given n, k, and the number of blocks of cheese at each grid location, compute the maximum amount of cheese FatMouse can eat before being unable to move.
FatMouse begins by standing at location (0,0). He eats up the cheese where he stands and then runs either horizontally or vertically to another location. The problem is that there is a super Cat named Top Killer sitting near his hole, so each time he can run at most k locations to get into the hole before being caught by Top Killer. What is worse -- after eating up the cheese at one location, FatMouse gets fatter. So in order to gain enough energy for his next run, he has to run to a location which have more blocks of cheese than those that were at the current hole.
Given n, k, and the number of blocks of cheese at each grid location, compute the maximum amount of cheese FatMouse can eat before being unable to move.
Input
a line containing two integers between 1 and 100: n and k
n lines, each with n numbers: the first line contains the number of blocks of cheese at locations (0,0) (0,1) ... (0,n-1); the next line contains the number of blocks of cheese at locations (1,0), (1,1), ... (1,n-1), and so on.
The input ends with a pair of -1's.
Output
Sample Input
3 1 1 2 5 10 11 6 12 12 7 -1 -1
Sample Output
37
题目意思大概是给你一个矩阵,每个点都有数目不一的奶酪,你每次最多不能跑超过k步且只能跑到奶酪比当前位置多的地方,问最多能吃多少奶酪。
这道题的dp反倒好想,用dp[][]储存每个位置最多能吃多少奶酪。问题是吃奶酪的路径。
很自然的想法是用dfs进行暴力搜索,然后求出每步最大值。但是这样会t,因为有很多条路径被重复计算了。这时候就要使用记忆化搜索。所谓记忆化搜索,按我的理解就是对每个点/路径进行标记,这样在求到重复路径时可以直接返回值,避免重复递归计算。实际实现很简单,把dp数组全部初始化为0,这样如果dp已经被计算过了,一定是不为0的(其实不够精确,因为可能有0块奶酪的点),所以dp不为0时直接返回原值就行。
AC代码:
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
int n,m,a[110][110],dp[110][110],dir[4][2]={0,1,0,-1,1,0,-1,0};
int dfs(int c,int d)
{
int x,y,t,maxx=0;
if(dp[c][d]) return dp[c][d];
for(int i=0;i<4;i++)
{
for(int j=1;j<=m;j++)
{
x=c+dir[i][0]*j;
y=d+dir[i][1]*j;
if(x>=0&&y>=0&&x<n&&y<n)
{
if(a[x][y]>a[c][d])
{
t=dfs(x,y);
maxx=max(maxx,t);
}
}
}
}
dp[c][d]=maxx+a[c][d];
return dp[c][d];
}
int main()
{
while(cin>>n>>m&&n!=-1&&m!=-1)
{
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
scanf("%d",&a[i][j]);
memset(dp,0,sizeof(dp));
int maxx=dfs(0,0);
cout<<maxx<<endl;
}
return 0;
}