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.

InputThere 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. 
OutputFor 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

题意:
一步最多走k步,只能走到比当前值大的位置,问路径的权值最大和是多少?
思路:
相当明显的记忆化搜索,算是一个基本套路。
#include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<ctime>
#define fuck(x) cout<<#x<<" = "<<x<<endl;
#define ls (t<<1)
#define rs ((t<<1)+1)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = ;
const int inf = 2.1e9;
const ll Inf = ;
const int mod = ;
const double eps = 1e-;
const double pi = acos(-);
int dp[][];
int mp[][];
int n,k;
int dfs(int x,int y){
if(dp[x][y]){return dp[x][y];}
int &ret = dp[x][y];
ret=mp[x][y];
for(int i=;i<=k;i++){
if(i+x<=n&&mp[i+x][y]>mp[x][y]){ret=max(ret,dfs(i+x,y)+mp[x][y]);}
if(i+y<=n&&mp[x][i+y]>mp[x][y]){ret=max(ret,dfs(x,i+y)+mp[x][y]);}
if(x-i>=&&mp[x-i][y]>mp[x][y]){ret=max(ret,dfs(x-i,y)+mp[x][y]);}
if(y-i>=&&mp[x][y-i]>mp[x][y]){ret=max(ret,dfs(x,y-i)+mp[x][y]);} }
return ret;
}
int main()
{
while(scanf("%d%d",&n,&k)&&(~n)&&(~k)){
for(int i=;i<=n;i++){
for(int j=;j<=n;j++){
scanf("%d",&mp[i][j]);
dp[i][j]=;
}
}
dfs(,);
printf("%d\n",dp[][]);
}
return ;
}

HDU - 1078 FatMouse and Cheese (记忆化搜索)的更多相关文章

  1. HDU 1078 FatMouse and Cheese 记忆化搜索DP

    直接爆搜肯定超时,除非你加了某种凡人不能想出来的剪枝...555 因为老鼠的路径上的点满足是递增的,所以满足一定的拓补关系,可以利用动态规划求解 但是复杂的拓补关系无法简单的用循环实现,所以直接采取记 ...

  2. HDU 1078 FatMouse and Cheese (记忆化搜索)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1078 老鼠初始时在n*n的矩阵的(0 , 0)位置,每次可以向垂直或水平的一个方向移动1到k格,每次移 ...

  3. HDU 1078 FatMouse and Cheese (记忆化搜索+dp)

    详见代码 #include <iostream> #include <cstdio> #include <cstdlib> #include <memory. ...

  4. !HDU 1078 FatMouse and Cheese-dp-(记忆化搜索)

    题意:有一个n*n的格子.每一个格子里有不同数量的食物,老鼠从(0,0)開始走.每次下一步仅仅能走到比当前格子食物多的格子.有水平和垂直四个方向,每一步最多走k格,求老鼠能吃到的最多的食物. 分析: ...

  5. hdu 1078 FatMouse and Cheese 记忆化dp

    只能横向或竖向走...一次横着竖着最多k步...不能转弯的.... 为毛我的500+ms才跑出来... #include<cstdio> #include<iostream> ...

  6. HDU ACM 1078 FatMouse and Cheese 记忆化+DFS

    题意:FatMouse在一个N*N方格上找吃的,每一个点(x,y)有一些吃的,FatMouse从(0,0)的出发去找吃的.每次最多走k步,他走过的位置能够吃掉吃的.保证吃的数量在0-100.规定他仅仅 ...

  7. hdu1078 FatMouse and Cheese(记忆化搜索)

    转载请注明出处:http://blog.csdn.net/u012860063 题目链接:pid=1078" target="_blank">http://acm. ...

  8. hdu1078 FatMouse and Cheese —— 记忆化搜索

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1078 代码1: #include<stdio.h>//hdu 1078 记忆化搜索 #in ...

  9. P - FatMouse and Cheese 记忆化搜索

    FatMouse has stored some cheese in a city. The city can be considered as a square grid of dimension ...

随机推荐

  1. Ubuntu composer 安装thinkphp5 失败,报错:[ErrorException] mkdir(): Permission denied

    在Linux环境下,使用composer安装thinkphp5,安装时,报错:[ErrorException]               mkdir(): Permission denied : 看 ...

  2. Django--session(登录用)

    一.session的原理图 二.Django中session对象的设置/读取/删除及其他方法 三. Django--配置 settings.py中与session有关的参数 一.session的原理图 ...

  3. 阿里巴巴开源的Asynchronous I/O Design and Implementation

    Motivation I/O access, for the most case, is a time-consuming process, making the TPS for single ope ...

  4. 一探究竟:Namenode、SecondaryNamenode、NamenodeHA关系

    NameNode与Secondary NameNode 很多人都认为,Secondary NameNode是NameNode的备份,是为了防止NameNode的单点失败的,其实并不是在这样.文章Sec ...

  5. day5-python的文件操作-坚持就好

    目录摘要 文件处理 1.文件初识 2.文件的读操作 3.文件的写操作 4.文件的追加操作 5.文件的其他操作 6.文件的修改 正式开始 文件处理:写了这么多代码了,有的时候我们执行完成的结果想永久保存 ...

  6. day16-面向对象基础(三)

    今日摘要 今天主要整理一下这俩天学习的内容,面向对象也快学完了,深刻的认识到面向对象就是一个思想,怎么把思想理解了,其他也就不是什么事了 1.类的约束 2.类的类方法与静态方法 3.类的反射 4.类的 ...

  7. 监控glusterfs

    监控集群状态 [4ajr@elk1 scripts]$ cat glusterfs_peer_status.sh #!/bin/bash peer_status=`sudo gluster peer ...

  8. ansible 与 Jinja2的结合

    1.文件架构 [root@master template]# tree . ├── jinj2_test.yml ├── meta ├── tasks ├── templates │   └── te ...

  9. System.Threading.Timer如何正确地被Dispose

    System.Threading.Timer是.NET中一个定时触发事件处理方法的类(本文后面简称Timer),它背后依靠的是.NET的线程池(ThreadPool),所以当Timer在短时间内触发了 ...

  10. Linux下修改MySQL数据表中字段属性

    一.修改某个表的字段类型及指定为空或非空 alter table 表名称 change 字段名称 字段名称 字段类型 [是否允许非空]; alter table 表名称 modify 字段名称 字段类 ...