zoukankan      html  css  js  c++  java
  • HDU 1078 FatMouse and Cheese(记忆化搜索)

    FatMouse and Cheese

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

    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
     

    题目链接:HDU 1078

    题目中的k是指在当前位置可以最多直接从(x,y)跳到(x+k*dirx,y+k*diry),不是指只能总体上在直线上平移多少距离,比如一条直线3 2 4,若以3为起点当k=2时就可以直接从3跳到4,不然由于无法到小于当前格子食物数量的位置就只能是3了,跳过去就可以得到3+4=7的食物量。然后这题跟滑雪又不一样,滑雪是可以从任何一个位置为起点,这题只能从(0,0)开始,因此最后不是取max(样例很巧合地取max也是37,WA N次)而是直接输出dp[0][0]

    代码:

    #Include<stdio.h>
    #include<bits/stdc++.h>
    using namespace std;
    #define INF 0x3f3f3f3f
    #define CLR(x,y) memset(x,y,sizeof(x))
    #define LC(x) (x<<1)
    #define RC(x) ((x<<1)+1)
    #define MID(x,y) ((x+y)>>1)
    typedef pair<int,int> pii;
    typedef long long LL;
    const double PI=acos(-1.0);
    const int N=110;
    int pos[N][N],dir[4][2]={{0,1},{0,-1},{1,0},{-1,0}};
    int dp[N][N];
    int n,k;
    inline bool check(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];
        else
        {
            int maxm=0;
            for (int s=1; s<=k; ++s)
            {
                for (int i=0; i<4; ++i)
                {
                    int xx=x+s*dir[i][0];
                    int yy=y+s*dir[i][1];
                    if(check(xx,yy)&&pos[xx][yy]>pos[x][y])
                        maxm=max<int>(maxm,dfs(xx,yy));
                }
            }
            return dp[x][y]=maxm+pos[x][y];
        }
    }
    int main(void)
    {
        int i,j;
        while (~scanf("%d%d",&n,&k)&&(n!=-1&&k!=-1))
        {
            CLR(dp,0);
            for (i=0; i<n; ++i)
                for (j=0; j<n; ++j)
                    scanf("%d",&pos[i][j]);
            for (i=0; i<n; ++i)
                for (j=0; j<n; ++j)
                    dfs(i,j);
            printf("%d
    ",dp[0][0]);
        }
        return 0;
    }
  • 相关阅读:
    科学-化学:化学百科
    科学-物理:物理学 (自然科学学科)百科
    科学-建筑学-建筑美学:建筑美学百科
    科学-建筑学:建筑学百科
    科学-哲学-美学:美学(中国哲学二级学科)
    哲学:哲学(世界观学说、社会形态之一)
    科学-语文:语文(语言和文学的简称)
    科学-分析:分析
    建模:数学建模
    科学-数学:数学
  • 原文地址:https://www.cnblogs.com/Blackops/p/5876206.html
Copyright © 2011-2022 走看看