码迷,mamicode.com
首页 > 其他好文 > 详细

HDU1078 ZOJ1107 FatMouse and Cheese【记忆化搜索】

时间:2019-01-25 11:38:52      阅读:120      评论:0      收藏:0      [点我收藏+]

标签:tor   most   set   sts   vertica   NPU   continue   name   red   

FatMouse and Cheese

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

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

Source
Zhejiang University Training Contest 2001

问题链接HDU1078 ZOJ1107 FatMouse and Cheese
问题简述:(略)
问题分析
????老鼠每次只能走k步停下来,停下的位置只能比上一个位置大,并且获取其价值。老鼠只能水平或垂直方向走,求能获得的最大价值。
????暴力搜索是必要,然而算过了的不重复计算是关键,所以采用记忆化搜索。
程序说明
????给出2个代码,前一种要简洁一些。
参考链接:(略)
题记:(略)

AC的C语言程序如下:

/* HDU1078 ZOJ1107 FatMouse and Cheese */

#include <algorithm>
#include <stdio.h>
#include <string.h>

using namespace std;

#define N 100
int n, k;
int a[N][N], dp[N][N];

int dfs(int row, int col)
{
    if(dp[row][col])
        return dp[row][col];
    else {
        dp[row][col] = a[row][col];
        for(int i = 1; i <= k; i++) {
            if(row + i < n && a[row + i][col] > a[row][col])
                dp[row][col] = max(dp[row][col], a[row][col] +dfs(row + i, col));
            if(row - i >= 0 && a[row - i][col] > a[row][col])
                dp[row][col] = max(dp[row][col], a[row][col] +dfs(row - i, col));
            if(col + i < n && a[row][col + i] > a[row][col])
                dp[row][col] = max(dp[row][col], a[row][col] +dfs(row, col + i));
            if(col - i >= 0 && a[row][col - i] > a[row][col])
                dp[row][col] = max(dp[row][col], a[row][col] +dfs(row, col - i));
        }
        return dp[row][col];
    }
}

int main(void)
{
    while(scanf("%d%d", &n, &k) != EOF && (n != -1 || k != -1)) {
        for(int i = 0; i < n; i++)
            for(int j = 0; j < n; j++)
                scanf("%d", &a[i][j]);

        memset(dp, 0, sizeof(dp));
        printf("%d\n", dfs(0, 0));
    }

    return 0;
}

AC的C语言程序如下:

/* HDU1078 ZOJ1107 FatMouse and Cheese */

#include <algorithm>
#include <stdio.h>
#include <string.h>

using namespace std;

#define K 4
int drow[] = {1, -1, 0, 0};
int dcol[] = {0, 0, 1, -1};

#define N 100
int n, k;
int a[N + 1][N + 1], dp[N + 1][N + 1];

int dfs(int row, int col)
{
    int ans = 0;
    if(!dp[row][col]) {
        for(int i = 1; i <= k; i++) {
            for(int j = 0; j < K; j++) {
                int nextrow = row + drow[j] * i;
                int nextcol = col + dcol[j] * i;
                if(nextrow < 1 || nextrow > n || nextcol < 1 || nextcol > n)
                    continue;
                if(a[nextrow][nextcol] > a[row][col])
                    ans = max(ans, dfs(nextrow, nextcol));
            }
        }
        dp[row][col] = a[row][col] + ans;
    }
    return dp[row][col];
}

int main(void)
{
    while(scanf("%d%d", &n, &k) != EOF && (n != -1 || k != -1)) {
        for(int i = 1; i <= n; i++)
            for(int j = 1; j <= n; j++)
                scanf("%d", &a[i][j]);

        memset(dp, 0, sizeof(dp));
        printf("%d\n", dfs(1, 1));
    }

    return 0;
}

HDU1078 ZOJ1107 FatMouse and Cheese【记忆化搜索】

标签:tor   most   set   sts   vertica   NPU   continue   name   red   

原文地址:https://www.cnblogs.com/tigerisland45/p/10317826.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!