其他分享
首页 > 其他分享> > FatMouse and Cheese

FatMouse and Cheese

作者:互联网

FatMouse and Cheese (记忆化)

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 Specification

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 Specification

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
Output for Sample Input

37

翻译

胖老鼠在一个城市里储存了一些奶酪。城市可以被视为一个尺寸为n的方格网:每个方格网位置都贴上了标签(p,q),其中0<=p<n和0<=q<n。在每个方格网位置,肥鼠在一个洞里藏了0到100块奶酪。现在他要享受他最喜欢的食物了。

胖老鼠从站在位置(0,0)开始。他把站着的奶酪吃掉,然后水平或垂直地跑到另一个地方。问题是有一只超级猫,名叫顶级杀手坐在他洞附近,所以每次他都能跑到最多K的地方,在被顶级杀手抓住之前进入洞里。更糟糕的是——在一个地方吃了奶酪之后,胖老鼠变得更胖。因此,为了获得足够的能量来进行下一次跑步,他必须跑到一个比当前洞里的奶酪块多的地方。

给定n、k和每个网格位置的奶酪块数,计算出脂肪鼠在无法移动之前可以吃的奶酪的最大数量。

输入规范

有几个测试用例。每个测试用例包括

包含介于1和100之间的两个整数的行:n和k
n行,每个行有n个数字:第一行包含位置(0,0)(0,1)处的奶酪块数…(0,n-1);下一行包含位置(1,0)、(1,1)、…(1,n-1)等等。
输入以一对-1结尾。
输出规格

对于一行中的每个测试用例输出,单个整数给出收集的奶酪块数。

样本输入

3 1
1 2 5
10 11 6
12 12 7
-1 -1
样本输入输出

37

思路

问题大意是有一只老鼠在起点(0,0)处,它只能横向或竖向走,每次跑的跑到一个点的时候他都要吃奶酪,但是每次奶酪都要比上一个要大,不然就会维持不了能量。要求输出它能吃到的最多的奶酪。
这题的解题思路是记忆化搜索。dfs(x,y)表示从点(x,y)遍历,用dp[x][y]保存点(x,y)处到结束为止时吃到的奶酪的最大值。在遍历的过程中用ans暂时表示(x,y)下一个能走到的位置(xx,yy)处到结束为止可以吃到的奶酪的最大值。
那么dp[x][y] = maxx+mapp[x][y];


#include<stdio.h>
#include<string.h>
int mapp[110][110];
int dp[110][110];
int dir[4][2]={0,1,0,-1,1,0,-1,0};
int n,k;
int dfs(int sx,int sy)
{
   int i,j,tx,ty,maxx=0,temp;
   if(dp[sx][sy]!=-1)      
      return dp[sx][sy];
   for(i=0;i<4;i++)      
     for(j=1;j<=k;j++)
     {
        tx=sx+dir[i][0]*j;
        ty=sy+dir[i][1]*j;
        if(tx<0||tx>=n||ty<0||ty>=n||mapp[tx][ty]<=mapp[sx][sy])    
          continue;
        temp=dfs(tx,ty);
        if(temp>maxx)        
           maxx=temp;
     }
  dp[sx][sy]=mapp[sx][sy]+maxx;     
  return dp[sx][sy];
}
int main()
{
  int i,j;
  while(~scanf("%d%d",&n,&k))
  {
    if(n==-1&&k==-1)
       break;
    memset(dp,-1,sizeof(dp)); 
    for(i=0;i<n;i++)
      for(j=0;j<n;j++)
        scanf("%d",&mapp[i][j]);
     printf("%d\n",dfs(0,0));
  }
  return 0;
}

标签:Cheese,cheese,int,奶酪,FatMouse,location,dp
来源: https://blog.csdn.net/rg_lzm/article/details/97617845