hdu 1078 FatMouse and Cheese (dfs+记忆化搜索)

hdu 1078 FatMouse and Cheese (dfs+记忆化搜索)

大家好,又见面了,我是全栈君。

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




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~K)个在同一直线的位置,即不能拐弯走。

数组较大,用记忆化搜索

(类似于poj1088滑雪)

#include"stdio.h"
#include"string.h"
#include"queue"
#include"vector"
#include"stack"
#include"algorithm"
using namespace std;
#define N 105
#define max(a,b) (a>b?a:b)
int g[N][N],n,k;
int h[N][N];
int dir[4][2]={0,1,0,-1,-1,0,1,0};
int judge(int x,int y)
{
    if(x<0||x>=n||y<0||y>=n)
        return 0;
    return 1;
}
int dfs(int x,int y)
{
    if(h[x][y])
        return h[x][y];
    int i,j,u,v,t,sum=0,s1;
    t=g[x][y];
    for(i=0;i<4;i++)
    {
        for(j=1;j<=k;j++)
		{
			u=x+dir[i][0]*j;
            v=y+dir[i][1]*j;
            if(judge(u,v)&&g[u][v]>t)
            {
                s1=dfs(u,v);
                sum=max(sum,s1);
            }
		}
    }
    return h[x][y]=g[x][y]+sum;
}
int main()
{
    int i,j;
    while(scanf("%d%d",&n,&k),n!=-1||k!=-1)
    {
        for(i=0;i<n;i++)
        {
            for(j=0;j<n;j++)
            {
                scanf("%d",&g[i][j]);
                h[i][j]=0;
            }
        }

        int ans=dfs(0,0);
        printf("%d\n",ans);
    }
    return 0;
}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/115377.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • ORM的详解

    ORM的详解

    2021年11月7日
    59
  • 数电和模电的理解「建议收藏」

    数电和模电的理解「建议收藏」模电模拟信号:随处可见的自然信号都是模拟信号,模拟信号在时间上和取值上都是连续的,画出来就是一条连续的曲线,可以完全地“模拟”自然信号。模电是指用来对模拟信号进行传输、变换、处理、放大、测量和显示等工作的电路。模拟信号是指连续变化的电信号。模拟电路是电子电路的基础,它主要包括放大电路、信号运算和处理电路、振荡电路、调制和解调电路及电源等。数电数字信号:在时间上和取值上都是不连续的。数字信号存在“采样”,数字信号的值只能在采样点变化。数字信号存在“量化”,数字信号的值只

    2022年6月20日
    47
  • SSL协议原理

    SSL协议原理

    2021年4月14日
    130
  • idc机房设施运维_软件运维需要掌握的知识

    idc机房设施运维_软件运维需要掌握的知识机房的服务器的维护是机房运维工作的重点,合理的机房环境对于服务器来说是非常的重要的,随着这年经济的发展,机房也在不断的在很多的方面进行调整,今天我们学习IDC机房服务器运维基础知识。1、关于电力(1)定期检测机房内市电及UPS电源是否稳定,并做好记录,UPS巡检记录要落实到个人。确保服务器硬件系统的稳定运转,确保市电中断后服务器正常运转理论值8小时。(2)若遇市电中断,如无特殊事宜,请尽量…

    2025年9月17日
    4
  • pycharm搭配anaconda_在anaconda中安装pycharm

    pycharm搭配anaconda_在anaconda中安装pycharm为了以后自己忘记可以不用再去找方法,在此记下步骤一、为已存在的project设置环境File–Settings–Pythoninterpreter找到自己Anaconda下的python.exe这里勾选makeavailabletoallprojects在这里就可以看到这个环境里有什么包了,可以看到高亮部分有pandas二、newproject在这里要选择previouslyconfiguredinterpreter点击右边的…进行设置在Cond

    2022年8月29日
    2
  • 最新QT下载和安装 指南教程「建议收藏」

    最新QT下载和安装 指南教程「建议收藏」原文地址:http://c.biancheng.net/view/3851.htmlQt体积很大,有1GB~3GB,官方下载通道非常慢,相信很多读者会崩溃,所以建议大家使用国内的镜像网站(较快),或者使用迅雷下载(很快)。作为Qt下载教程,本文会同时讲解以上三种下载方式。Qt官方下载(非常慢)Qt官网有一个专门的资源下载网站,所有的开发环境和相关工具都可以从这里下载,具体地址是:http://download.qt.io/图1:Qt官方下载网站截图对目录结构的…

    2022年5月17日
    37

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号