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 Specification

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 Specification

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

Output for Sample Input

37

题意:给出一个n和k,代表一个n*n的矩阵,k代表老鼠每一次水平或者垂直走的最多步数,-1-1输入结束

没写出来的原因:

  1. 对于记忆化搜索理解不够透彻,这一题不需要对于走过的点进行标记。每次走到之前走过的那个位置,直接返回那个位置所对应的奶酪数,因为那个点所找的奶酪数肯定是之前找过的并且是最多的。
  2. 对于每次可以走1、2、...k步不知道可以在控制方向那边再用一层循环去进行控制。
 #include<stdio.h>
#include<iostream>
#include<cmath>
#include<string.h>
#include<iomanip>
using namespace std; int n,k;
int dir[][]= {{,},{,-},{-,},{,}};
int a[][];
int ss[][];//存每一步能够得到的最大奶酪数,之后要是访问过就直接返回 int dfs(int x,int y)
{
if(ss[x][y])
return ss[x][y];
else
{
for(int i=; i<; i++)
{
for(int j=; j<=k; j++)
{
int tx=x+dir[i][]*j;
int ty=y+dir[i][]*j;
// if(tx>=0&&tx<n&&ty>=0&&ty<n&&ss[x][y]==0)//WA//之前这个点可能是已经记录过步数的,因为要通过两层循环去控制每次的步数和方向,然后去寻找这个点的最大值进行更新
if(tx>=&&tx<n&&ty>=&&ty<n)
{
if(a[tx][ty]>a[x][y])
{
int maxx=dfs(tx,ty);
if(maxx>ss[x][y])
{
ss[x][y]=maxx;
}
}
}
}
}
ss[x][y]=ss[x][y]+a[x][y];
return ss[x][y];
}
}
int main()
{
std::ios::sync_with_stdio(false);
cin.tie();
cout.tie();
while(cin>>n>>k)
{
memset(a,,sizeof(a));
memset(ss,,sizeof(ss));
if(n==-&&k==-)
break;
for(int i=; i<n; i++)
{
for(int j=; j<n; j++)
{
cin>>a[i][j];
}
}
cout<<dfs(,)<<endl;
}
return ;
}

ZOJ-1107-FatMouse and Cheese-dfs+记忆化搜索的更多相关文章

  1. zoj 1107 FatMouse and Cheese(记忆化搜索)

    题目链接:点击链接 题目大意:老鼠从(0,0)出发,每次在同一个方向上最多前进k步,且每次到达的位置上的数字都要比上一个位置上的数字大,求老鼠经过的位置上的数字的和的最大值 #include<s ...

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

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1078 题目大意: 题目中的k表示横向或者竖直最多可曾经进的距离,不可以拐弯.老鼠的出发点是(1,1) ...

  3. 随手练——HDU 1078 FatMouse and Cheese(记忆化搜索)

    http://acm.hdu.edu.cn/showproblem.php?pid=1078 题意: 一张n*n的格子表格,每个格子里有个数,每次能够水平或竖直走k个格子,允许上下左右走,每次走的格子 ...

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

    FatMouse and Cheese Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe ...

  5. ZOJ 3644 Kitty's Game dfs,记忆化搜索,map映射 难度:2

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4834 从点1出发,假设现在在i,点数为sta,则下一步的点数必然不能是sta的 ...

  6. HDU 1078 FatMouse and Cheese【记忆化搜索】

    题意:给出n*n的二维矩阵,和k,老鼠每次最多走k步,问老鼠从起点(0,0)出发,能够得到的最大的数(即为将每走过一点的数都加起来的和最大)是多少 和上一题滑雪一样,搜索的方向再加一个循环 #incl ...

  7. 不要62 hdu 2089 dfs记忆化搜索

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=2089 题意: 给你两个数作为一个闭区间的端点,求出该区间中不包含数字4和62的数的个数 思路: 数位dp中 ...

  8. dfs+记忆化搜索,求任意两点之间的最长路径

    C.Coolest Ski Route 题意:n个点,m条边组成的有向图,求任意两点之间的最长路径 dfs记忆化搜索 #include<iostream> #include<stri ...

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

    pid=1078">FatMouse and Cheese Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/ ...

  10. kuangbin专题十二 HDU1078 FatMouse and Cheese )(dp + dfs 记忆化搜索)

    FatMouse and Cheese Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe ...

随机推荐

  1. filter的使用场景

    1.filter的使用场景:假如有一个对象数组A,获取数组中的指定类型的对象放到B数组中 我们在ES5先进行for循环遍历数组,再进行if 判断,如果数组中某个对象的类型符合要求,push 到一个新数 ...

  2. docker gitlab backup

    说明:下面命令中带有<your container name>字,是 gitlab 容器的名称,请按实际情况进行代替 在创建备份之前,你不需要停止任何东西 docker exec -t & ...

  3. Optimal Marks SPOJ - OPTM

    传送门 一个无向图,每个点有点权,某些点点权确定了,某些点由你来确定,边权为两个点的异或和,要使边权和最小. 这不是一道按位做最小割的大水题么 非常开心地打了,还非常开心地以为有spj,然后非常开心地 ...

  4. OpenSceneGraph | OSG如何存储带纹理osgb格式可以节省空间

      在使用OSG(OpenSceneGraph)存储带纹理osgb格式的过程中,大家会遇到这样一种情况:存储后的osgb文件所占用的大小远大于原始文件的大小,几倍至几十倍.这是为何呢?原因是OSG默认 ...

  5. NX二次开发-创建直线(起点-向量方向-长度)UF_CURVE_create_line

    NX9+VS2012 #include <uf.h> #include <uf_curve.h> #include <uf_csys.h> #include < ...

  6. hdu多校第五场1002 (hdu6625) three arrays 字典树/dfs

    题意: 给你两个序列a,b,序列c的某位是由序列a,b的此位异或得来,让你重排序列ab,找出字典序最小的序列c. 题解: 如果能找到a,b序列中完全一样的值当然最好,要是找不到,那也尽量让低位不一样. ...

  7. 对A盾原理的小小总结,膜拜A神

    A盾的原理是在驱动加载时重载os内核,获取原始ssdt表的地址. 应用层点击查询的代码在文件A-ProtectView.cpp中,每种点击操作调用相应的 query查询函数,在query函数里 Rea ...

  8. ionic-CSS:ionic Range

    ylbtech-ionic-CSS:ionic Range 1.返回顶部 1. ionic Range ionic Range 是一个滑块控件,ionic 为 Range 提供了很多种默认的样式.而且 ...

  9. /lib/libmysqlcppconn.so: undefined reference to `mysql_stmt_execute@libmysqlclient_18' 解决方法

    sudo apt-get install libmysqlcppconn-dev

  10. beaglebone black 与电脑互传文件(夹)

    1. PSCP方法 PSCP,是putty的一个组件. 1.1 下载PSCP 先给连接http://www.chiark.greenend.org.uk/~sgtatham/putty/downloa ...