zoukankan      html  css  js  c++  java
  • hdu 1078(记忆化搜索)

    FatMouse and Cheese

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


    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,k,n代表n*n的矩阵,矩阵每个点都有一个权值(奶酪数),一只老鼠从0 0 点出发,每次可以向上下左右某个方向走1-K步,但要保证下一个点的权值一定要比上一个点大,求老鼠能得到的最大奶酪数。
    题解:记忆化搜索其本质是深搜和动态规划的结合。用一个dp数组保存当前点的最优解,先递归找到子问题的最优解ans,然后回溯的时候 更新
    dp数组 = 当前点的权值+子问题的最优(大)解。
    #include<iostream>
    #include<cstdio>
    #include<algorithm>
    #include <string.h>
    #include <math.h>
    using namespace std;
    
    int n,k;
    int mp[101][101];
    int dp[101][101];///保存当前点最大的价值
    int mx;
    int dir[4][2] = {{1,0},{-1,0},{0,1},{0,-1}};
    
    int dfs(int x,int y){
        int ans = 0;  ///最开始的子问题最优解为0
        if(dp[x][y]!=-1) return dp[x][y];
        for(int i=1;i<=k;i++){  ///枚举每次走的步数
            for(int j=0;j<4;j++){
                int nextx = x+dir[j][0]*i;
                int nexty = y+dir[j][1]*i;
                if(nextx<0||nextx>n-1||nexty<0||nexty>n-1) continue;
                if(mp[nextx][nexty]>mp[x][y])
                    ans = max(ans,dfs(nextx,nexty));
            }
        }
        dp[x][y]=mp[x][y]+ans;///递归回来的时候,ans保存的是子问题的最优解,然后加上自身的
                              ///值,便是当前问题的最优解
        return dp[x][y];
    }
    int main()
    {
        while(scanf("%d%d",&n,&k)!=EOF,n!=-1&&k!=-1){
            mx = -1;
            for(int i=0;i<n;i++){
                for(int j=0;j<n;j++) scanf("%d",&mp[i][j]);
            }
            memset(dp,-1,sizeof(dp));
            mx = dfs(0,0);
            printf("%d
    ",mx);
        }
        return 0;
    }
     
  • 相关阅读:
    linux weblogic的sh文件
    linux 安装weblogic(转载)
    linux 安装jdk
    linux 用户和用户组
    测试开发工程师必备软硬能力&高级测试开发工程师需要具备什么能力?
    postman强大的团队协作功能
    requests(一): 发送一个json格式的post请求
    python操作Excel模块openpyxl
    appium环境安装app自动化
    夜神模拟器怎么连接adb
  • 原文地址:https://www.cnblogs.com/liyinggang/p/5370054.html
Copyright © 2011-2022 走看看