zoukankan      html  css  js  c++  java
  • FatMouse and Cheese 动态化搜索

    FatMouse and Cheese

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


    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
     动态化搜索,详见代码。
     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<cmath>
     5 #include<queue>
     6 #include<algorithm>
     7 using namespace std;
     8 int n,k;
     9 const int maxn = 105;
    10 int maze[maxn][maxn],dp[maxn][maxn];
    11 int dfs(int p,int q){
    12     if(dp[p][q]) return dp[p][q];
    13     int l,r,d,u;
    14     int ans = 0,maxx = 0;
    15     l = p - k;
    16     r = p + k;
    17     if(l<0) l = 0;
    18     if(r>=n) r = n-1;
    19     for(int i = l; i<=r; i++){
    20         if(maze[i][q]>maze[p][q]){
    21             ans = dfs(i,q);
    22             if(ans>maxx) maxx = ans;
    23         }
    24     }
    25     u = q + k;
    26     d = q - k;
    27     if(d<0) d = 0;
    28     if(u>=n) u = n-1;
    29     for(int i = d; i<=u; i++){
    30         if(maze[p][i]>maze[p][q]){
    31             ans = dfs(p,i);
    32             if(ans>maxx) maxx = ans;
    33         }
    34     }
    35     dp[p][q] = maxx + maze[p][q];
    36     return dp[p][q];
    37 }
    38 void input(){
    39         while(scanf("%d%d",&n,&k)!=EOF&&n != -1&&k != -1){
    40             for(int i = 0; i<n; i++)
    41                 for(int j = 0; j<n; j++)
    42                 scanf("%d",&maze[i][j]);
    43             memset(dp,0,sizeof(dp));
    44             printf("%d
    ",dfs(0,0));
    45         }
    46 }
    47 int main()
    48 {
    49     input();
    50     return 0;
    51 }
    卷珠帘
  • 相关阅读:
    自定义的弹出框列表适配器,类似于大众点评或美团
    Android 微信支付&支付宝支付
    动态设置 view 在布局中位置
    android 之图片异步加载
    android 侧滑菜单
    Google 官方 侧滑 drawerlayout
    python D27网络传输协议
    计算机单位换算、以及sort、sorted函数的区别
    python D26 socket、网络整个通信流程
    python D25 包
  • 原文地址:https://www.cnblogs.com/littlepear/p/5385164.html
Copyright © 2011-2022 走看看