FatMouse and Cheese(dp+dfs)

问题描述:
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

题意:

问题大意是有一只老鼠在起点(0,0)处,它只能横向或竖向走,每次跑的跑到一个点的时候他都要吃奶酪,但是每次奶酪都要比上一个要大,不然就会维持不了能量。要求输出它能吃到的最多的奶酪。

这题的解题思路是记忆化搜索。dfs(x,y)表示从点(x,y)遍历,用dp[x][y]保存点(x,y)处到结束为止时吃到的奶酪的最大值。在遍历的过程中用ans暂时表示(x,y)下一个能走到的位置(xx,yy)处到结束为止可以吃到的奶酪的最大值。 
那么dp[x][y] = ans+mp[x][y];

代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1005;
int n,k;
int mp[N][N];
int dir[4][2] = {-1,0,0,-1,1,0,0,1};//用于四个方向的移动 
int dp[N][N];

int dfs(int x,int y){
    int ans = 0;
    if(!dp[x][y]){
        for(int i = 0;i < 4;i++){  //4是指上下左右四个方向 
            for(int j = 1;j <= k;j++){
                //走的步数遍历 
                int xx = x + dir[i][0]*j;
                int yy = y + dir[i][1]*j;
                if(xx >= 0 && xx < n &&yy >= 0 && yy < n){  //前提要保证不出界 
                    if(mp[xx][yy] > mp[x][y])
                    //ans = max(ans,dp[xx][yy]) 
                    ans = max(ans,dfs(xx,yy));   //dfs(不太懂) 
                }
            }
        }
        dp[x][y] = ans + mp[x][y];   //dp[x][y]指从当前节点mp[x][y]开始(向后)所能吃的最多奶酪数 
    }
    return dp[x][y];
}

int main(){

    while(cin>>n>>k){
        if(n == -1 && k == -1){
            break;
        }
        for(int i = 0;i < n;i++){
            for(int j = 0;j < n;j++){
                cin>>mp[i][j];
            }
        }
        memset(dp,0,sizeof(dp));
        cout<<dfs(0,0)<<endl;
    }
    return 0;
}


阿里云建站—为企业提供互联网“快”服务
2020年因为一场突如其来的疫情,不少企业受到了严重冲击,疫情的冲击力对传统“纯线
下”行业的危害最大,互联网女皇玛丽·米克(MaryMeeker)4月17日发布了著名的年度互
联网趋势报告,报告中指出:拥有强大的互联网线上线下融合能力的企业在疫情中的表现最好,
线上线下融合的趋势已经存在一段时间了,但是疫情让这种需求变得更加的迫切。
如果你的企业完全依附于传统的、纯线下的经验模式,那么在2020年你将“必死无疑”,
一场巨大的,前所未有的互联网革命已经到来!
阿里云建站为了助力各行各业复工复产,疫情期间“马不停蹄”为数以万计的企业快速完成
建站,为他们朝着“线上线下融合”或者“纯线上”的互联网经营模式迈进,打下了坚实的基础。
“云·速成美站”模板建站1天就能上线,不懂技术没关系,打字就能建网站。千套网站模
板免费提供,百元就能建官网,一价全包,无任何隐形消费。
“云·企业官网”定制建站1周就能上线,高端量身定制建站,千元建官网无需自己动手,
建站专家1对1网站策划及设计,专业省心之选。
疫情,是一场大浪淘沙,每一次危机背后都隐藏着机会,危机越大,机会也越大。大环境
已经改变,如果你不努力不去改变来迎合这个大环境,那你必将被淘汰。
阿里云助力企业建站,优惠多多,福利多多,详情请点击如下链接
https://www.aliyun.com/minisite/goods?userCode=tz7kl4ag

猜你喜欢

转载自blog.csdn.net/weixin_44075041/article/details/99826241