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.
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.
3 1 1 2 5 10 11 6 12 12 7 -1 -1
37这个题的意思呢,,,。是一只小老鼠,要外出偷粮,然后呢,在每个粮仓K步远处有一只猫。所以,在每个粮仓附近最多走K步远(也不知道为什么他们只看守那一个粮仓
![大笑](http://static.blog.csdn.net/xheditor/xheditor_emot/default/laugh.gif)
注意:他是沿一个方向最多走K步,而不是单单是总共的步数(不能走着走着拐歪了,这样不可以)。
然后呢,每走一步,就会消耗能量,数值就是。然后呢,也比较贪心,只往大的方向走。问走不动时,最多吃粮多少???
大概这个意思啦||||
推荐看一下:HDU1978;http://blog.csdn.net/zitian246/article/details/78008167
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int n,k;
int mapp[120][120];
int dp[120][120];
int dir[4][2]= {0,1,1,0,0,-1,-1,0};
int dfs(int x,int y)
{
if(dp[x][y])
return dp[x][y];
int ms=0,s,i,j;
int tx,ty;
for(i=0; i<4; i++)
{
for(j=1; j<=k; j++)
{
tx=x+dir[i][0]*j; //只能沿一个方向走!!!
ty=y+dir[i][1]*j; //同上,可以算走的步数
if(tx<0||ty<0||tx>=n||ty>=n)continue;
if(mapp[tx][ty]<=mapp[x][y])continue;
ms=max(ms,dfs(tx,ty)); //求最大值。
}
dp[x][y]=mapp[x][y]+ms; //。。。。。。。
}
return dp[x][y];
}
int main()
{
while(~scanf("%d%d",&n,&k))
{
if(n==-1&&k==-1)break;
memset(dp,0,sizeof(dp));
for(int i=0; i<n; i++)
{
for(int j=0; j<n; j++)
{
scanf("%d",&mapp[i][j]);
}
}
printf("%d\n",dfs(0,0));
}
return 0;
}