• 台州 OJ FatMouse and Cheese 深搜 记忆化搜索


    3054: FatMouse and Cheese 分享至QQ空间

    时间限制(普通/Java):1000MS/3000MS     内存限制:65536KByte
    总提交: 129            测试通过:78

    描述

     

    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.

    输入

     

    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.

    输出

    For each test case output in a line the single integer giving the number of blocks of cheese collected.

     

     

    有一只老鼠,每次可以走(1~k)步,每一次行动所到达的格子的奶酪数量必须大于这一次行动开始的格子的奶酪数量,求这只老鼠最多可以吃到多少块奶酪。

    用一个数组存放从(x, y) 坐标开始,最多可以吃多少块奶酪。因为下一次吃到的奶酪数量肯定要比前一次多,所以无论之前是以什么路径到达了(x, y) 这个位置,如果(x, y) 求过了,就不用再求,直接返回对应的值就好了。

     

    #include <iostream>
    #include <cstring>
    using namespace std;
    
    const int MAX = 105;
    
    int n, k;
    int dp[MAX][MAX];        //在 (i, j) 点最大能吃到的奶酪和
    int G[MAX][MAX];
    int dx[4] = {-1, 0, 1, 0};
    int dy[4] = { 0, 1, 0,-1};
    
    int dfs(int r, int c);
    
    int main(){
        freopen("input.txt", "r", stdin);
        
        while(scanf("%d%d", &n, &k) && n+k >= 0){
            for(int i=1; i<=n; i++){
                for(int j=1; j<=n; j++){
                    scanf("%d", &G[i][j]);
                }
            }
            
            memset(dp, -1, sizeof(dp));
    //        dp[1][1] = G[1][1];
            printf("%d
    ", dfs(1, 1));
        }
        
        return 0;
    }
    
    int dfs(int r, int c){
        if(dp[r][c] != -1){
            return dp[r][c];
        }
        
        int maxNum = 0;
        for(int i=0; i<4; i++){
            for(int j=1; j<=k; j++){
                int x = r + dx[i]*j;
                int y = c + dy[i]*j;
                if(x < 1 || x > n || y < 1 || y > n)
                    break;
                if(G[x][y] > G[r][c]){
                    maxNum = max(maxNum, dfs(x, y));
                }
            }
        }
        maxNum += G[r][c];
        dp[r][c] = maxNum;
        return maxNum;
    }

     

     

  • 相关阅读:
    Oracle 数据库简介
    Qt 中事件与处理
    npm常用命令总结
    自适应宽度布局
    原生js发送ajax请求
    微信调试本地环境代码
    多行文本溢出显示省略号
    清除浮动
    用JQuery动态为选中元素添加/删除类
    input中加入搜索图标
  • 原文地址:https://www.cnblogs.com/lighter-blog/p/7344745.html
Copyright © 2020-2023  润新知