回溯 dp 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 locationis labelled (p,q) where 0 <= p < n and 0 <= q < n. At each gridlocation Fatmouse has hid between 0 and 100 blocks of cheese in a hole. Nowhe'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 horizontallyor vertically to another location. The problem is that there is a super Catnamed Top Killer sitting near his hole, so each time he can run at most klocations 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 inorder to gain enough energy for his next run, he has to run to a location whichhave more blocks of cheese than those that were at the current hole.

Given n, k, and the number of blocks ofcheese at each grid location, compute the maximum amount of cheese FatMouse caneat before being unable to move.

Input Specification

There are several test cases. Each testcase 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 pairof -1's.

Output Specification

For each test case output in a line thesingle 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 SampleInput

37

 

 

题目意思解读:一只老鼠去吃他藏在一个二维网上的食物,每一次最多跑k步,每次只能跑横向或纵向,既不能转弯,且每次停下来的位置的食物比前一次停的位置的多。

解题思路:本题涉及到的知识点有:回溯法、dp、以及深度优先搜索。具体来说,我们用value[i][j]来存储每个位置的食物数目,用key[i][j]表示从位置[i][j]出发能吃到的最多食物,那么key[0][0]就是在整个二维网中老鼠能吃到的最多的食物。则状态转移方程为:

key[x][y]=max{key[i][y],key[x][j]}+value[x][y]

其中i的范围是与x水平的距离小于k,j的范围是与y的垂直距离小于k,整个状态转移方程的前提条件是转移坐标的食物比当前坐标的食物多。

在整个dp过程中,可以说其搜索过程是基于图的深度优先搜索的回溯法。

整个解题的程序如下:

 

#include<iostream>

using namespacestd;

 

#define maxN 105

 

int N,k;

intvalue[maxN][maxN];

intkey[maxN][maxN];

int Pozition(intx,int y);

int main()

{

       cin>>N>>k;

       while((N!=-1)&&(k!=-1))

       {

              for(int i=0;i<N;i++)

              {

                     for(int j=0;j<N;j++)

                     {

                            cin>>value[i][j];

                            key[i][j]=0;

                     }

              }

              cout<<Pozition(0,0)<<endl;

              cin>>N>>k;

       }

       return 0;

}

int Pozition(intx,int y)

{

       if(key[x][y]==0)

       {

              int maxn=-1;

              int ans;

              int left=x-k,right=x+k;

              if(left<0)

                     left=0;

              if(right>=N)

                     right=N-1;

              for(int i=left;i<=right;i++)

              {

                     if(value[i][y]>value[x][y])

                     {

                            ans=Pozition(i,y);

                            if(ans>maxn)

                                   maxn=ans;

                     }

              }

              int up=y+k;

              int down=y-k;

              if(up>=N)

                     up=N-1;

              if(down<0)

                     down=0;

              for(int i=down;i<=up;i++)

              {

                     if(value[x][i]>value[x][y])

                     {

                            ans=Pozition(x,i);

                            if(ans>maxn)

                                   maxn=ans;

                     }

              }

              key[x][y]=maxn+value[x][y];

       }

       return key[x][y];

}

 

注:这个程序中应用的小技巧是对边界的判断,可以作为参考。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值