hdu1078FatMouse and Cheese解题报告---记忆化搜索

版权声明:转载请注明出处:https://blog.csdn.net/qq1013459920 https://blog.csdn.net/qq1013459920/article/details/84800732

                                       FatMouse and Cheese

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 14888    Accepted Submission(s): 6300

 

Problem Description

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步,可以选择上下左右四个方向移动,下一个移动点奶酪块数量必须要大于当前点。

可以想到是搜索,但是没有用数组去存储每次的最大数量就会dfs超时。

 记忆化搜索入门

AC Code: 

#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<cstdio>
#include<queue>
using namespace std;
static const int dir[4][2] = {{1, 0}, {-1, 0}, {0, -1}, {0, 1}};
int n, k;
int dp[105][105], vv[105][105];
inline int read(){int x = 0, f = 1;char ch = getchar();
    while(ch > '9' || ch < '0') {if(ch == '-') f = -1;ch = getchar();}
    while(ch >= '0' && ch <= '9'){x = x * 10 + ch - '0';ch = getchar();}
    return x * f;
}
bool is_bound(int x, int y){
    return (x >= 0 && x < n && y >= 0 && y < n);
}

int dfs(int x, int y){
    if(dp[x][y]) return dp[x][y];
    int res = 0;
    for(int t = 1; t <= k; t++){
        for(int i = 0; i < 4; i++){
            int fx = x + dir[i][0] * t;
            int fy = y + dir[i][1] * t;
            if(!is_bound(fx, fy)) continue;
            if(vv[fx][fy] > vv[x][y])
                res = max(res, dfs(fx, fy));
        }
    }
    dp[x][y] = res + vv[x][y];
    return dp[x][y];
}
int main(){
    while(scanf("%d%d", &n, &k) != EOF){
        memset(dp, 0, sizeof(dp));
        if(n == -1 && k == -1) break;
        for(int i = 0; i < n; i++){
            for(int j = 0; j < n; j++){
                scanf("%d", &vv[i][j]);
            }
        }
        printf("%d\n", dfs(0, 0));
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq1013459920/article/details/84800732