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.
Input
There are several test cases. Each test case consists of
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
For each test case output in a line the single integer giving the number of blocks of cheese collected.
Sample Input
3 1 1 2 5 10 11 6 12 12 7 -1 -1
Sample Output
37
本题问的啥:给你一个正方形的方格(n*n)上面放着棉花糖其数量不一,有一个老鼠在(0,0)位置出发去收其棉花糖,
这只老鼠走的最大步数不能超过k,但有一个要求是收的糖必须比前一个的数量多,最后问你这只老鼠能收的最多的棉花糖是多少?
//对于大佬来说这道题太简单了,可惜我是个菜鸟。。。
其代码解释在代码注释中。。。
#include
#include
#include
using namespace std;
int a[110][110];
int dp[110][110];
int n,k;
int d[4][2]={1,0,-1,0,0,-1,0,1};//搜索时的方向
int dfs(int x,int y){
if(dp[x][y]) return dp[x][y];//就是在返回(递归回来)算其最大值时如果在其之前已经搜索过就不用再次搜索了//防止TLE
int MAX=0;
for(int i=0;i<4;i++)//搜素时的方向//发现个玄学问题如果把i,j定义为全局变量的话会出现奇怪的现象//大佬解释下呗
for(int jump=1;jump<=k;jump++)
{
int ex=x+d[i][0]*jump;
int ey=y+d[i][1]*jump;//与dfs的套路一样
if(ex>=0&&ey>=0&&exa[x][y])//不能越界,符合此时的棉花糖的数量比前一个的多
{
int ans=dfs(ex,ey);//所谓的神搜其实就是递归递归
MAX=max(ans,MAX);//这里要格外要说一下(因为我是菜鸡)这里比较的最大值是递归回来的值与其ans进行的比较,可不是现在的你所谓的MAX=0的值
}
}
return dp[x][y]=MAX+a[x][y];//这个就是递归的出口和进口
}
int main()
{
while(scanf("%d%d",&n,&k)&&(n+k)!=-2)
{
memset(dp,0,sizeof(dp));
for(int i=0;i