首页 > 其他分享 >hdu:FatMouse and Cheese(记忆化非线性dfs)

hdu:FatMouse and Cheese(记忆化非线性dfs)

时间:2022-12-04 10:00:41浏览次数:39  
标签:Cheese hdu dfs cheese int ans FatMouse location each


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 本题最核心在于搜索使用的状态为从该点出发所得的最优解,避免了直接处理的不确定性 附ac代码

#include<bits/stdc++.h>
using namespace std;
int num[110][110],ans[110][110],n,k;
int dir[4][2]={{0,1},{0,-1},{1,0},{-1,0}};
bool pd(int x,int y)
{
    if(x>=0&&x<n&&y>=0&&y<n)
    return 1;
    else
    return 0;
}
int dfs(int x,int y)
{
    int answer=0;
    if(ans[x][y]) return ans[x][y];
    for(int i=1;i<=k;++i)
    {
        for(int j=0;j<4;++j)
        {
            int x1=x+dir[j][0]*i;
            int y1=y+dir[j][1]*i;
            if(pd(x1,y1)&&num[x1][y1]>num[x][y])
            answer=max(answer,dfs(x1,y1));
        }
    }
    ans[x][y]=num[x][y]+answer;
    return ans[x][y];
}
int main()
{
    while(scanf("%d%d",&n,&k)==2&&n>0)
    {
    memset(ans,0,sizeof(ans));
    for(int i=0;i<n;++i)
     for(int j=0;j<n;++j)
     {
         scanf("%d",&num[i][j]);
     }
    printf("%d\n",dfs(0,0));    
    }
}

 

标签:Cheese,hdu,dfs,cheese,int,ans,FatMouse,location,each
From: https://www.cnblogs.com/ruoye123456/p/16949440.html

相关文章

  • hdu:悼念512汶川大地震遇难同胞——选拔志愿者(回扣必胜点定义)
    ProblemDescription对于四川同胞遭受的灾难,全国人民纷纷伸出援助之手,几乎每个省市都派出了大量的救援人员,这其中包括抢险救灾的武警部队,治疗和防疫的医护人员,以及进行心......
  • hdu: Public Sale(博弈入门)
    ProblemDescription虽然不想,但是现实总归是现实,Lele始终没有逃过退学的命运,因为他没有拿到奖学金。现在等待他的,就是像FarmJohn一样的农田生涯。要种田得有田才行,Lele......
  • hdu:Fibonacci again and again(nim博弈与斐波那契)
    ProblemDescription任何一个大学生对菲波那契数列(Fibonaccinumbers)应该都不会陌生,它是这样定义的:F(1)=1;F(2)=2;F(n)=F(n-1)+F(n-2)(n>=3);所以,1,2,3,5,8,13……就是......
  • 【FastDfs】Docker自定义构建ARM架构的FastDfs镜像
    由于服务器环境为ARM架构,在部署fastdfs时,发现网上的镜像几乎都是X86_64的,不同架构的镜像还不能通用,这个就有点烦了。。。。。由于之前没有从头编译制作过镜像,步步都是坑,在......
  • HDU-5418 Floyd + DP
    题目传送门时间复杂度:\(O(2^n\cdotn^2)\)注意:输入尽量用scanf输入,输入需要记录两个路径的最小值代码:#include<iostream>#include<queue>#include<vector>#......
  • HDU 6273 Master of GCD(差分)
    题目分析贴一个别人的题解这个题就是一个差分数组,因为这数列的最大公约数就是这个数列2的出现2的最少次数的幂乘以3的出现3的最少次数的幂将2和3分开讨论,然后分......
  • hdu最佳编码(哈夫曼编码)
    ProblemDescription文本编码是计算机通信中的常见问题。以文本“AAAAABCD”为例,如果使用ASCII,则一共需要64位(因为每个字符的ASCII编码都是需要8位)。对应的,如果我们将......
  • hdu棋盘游戏(二分图匹配)
    题目描述ProblemDescription小希和Gardon在玩一个游戏:对一个N*M的棋盘,在格子里放尽量多的一些国际象棋里面的“车”,并且使得他们不能互相攻击,这当然很简单,但是Gardon限......
  • HDFS,MapReduce,Yarn 的架构思想和设计原理
        大家好,我是梦想家Alex。之前我也写了不少关于大数据技术组件的文章,例如:​​     前方高能|HDFS的架构,你吃透了吗?​​​​     MapReduce......
  • hadoop学习之HDFS API-2-通过编写java接口操作hdfs
    1.创建文件夹工程的test包中java->com.imooc.bigdata->hadoop.hdfs.HDFSApp注意包:importorg.apache.hadoop.conf.Configuration;importorg.apache.hadoop.fs.FileSyste......