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. 实时监听input输入情况

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  2. Shell case in语句详解

    和其它编程语言类似,Shell 也支持两种分支结构(选择结构),分别是 if else 语句和 case in 语句.在<Shell if else>一节中我们讲解了 if else 语句 ...

  3. SpringDataJpa实现自定义(更新)update语句

    SpringDataJpa的框架没有线程的更新方法,只能调用save()方法实行保存,如果是只更新一处的话,这个也不太适用.所以楼主尝试着自定义sql语句来写. service层 @Overridep ...

  4. bootstrapValidator--表单校验

    关于表单校验 要依次引入 <link rel="stylesheet" href="./bootstrap/css/bootstrap.min.css"& ...

  5. MySql 字段类型对应 Java 实体类型

    前言 MySQL Connector/J 对于 MySql 数据类型和 Java 数据类型之间的转换是很灵活的: 一般来讲,任何 MySql 数据类型都可以被转换为一个 java.lang.Strin ...

  6. 【Codeforces 1185C2】Exam in BerSU (hard version)

    [链接] 我是链接,点我呀:) [题意] 要让前i个数字的和小于等于M. 问你最少要删掉前i-1个数字中的多少个数字,每个询问都是独立的. [题解] ti的范围很小. 所以N*MAX(TI)暴力枚举就 ...

  7. 暑假集训test-8-30

    这套题有毒,T1标程挂了,T2题面完全莫名其妙,T3没有告诉取模害我打了好久高精... A题. 统计每个数后面比它小的数的个数记作f把,操作一个数就是把它后面所有比它小的数和它的f清0,然后若是它到它 ...

  8. NX二次开发-创建圆弧(圆心-半径)UF_CURVE_create_arc

    NX9+VS2012 #include <uf.h> #include <uf_curve.h> #include <uf_ui.h> #include <u ...

  9. NX二次开发-UFUN获得当前图纸页有多少个视图UF_DRAW_ask_num_views

    #include <uf.h> #include <uf_draw.h> #include <uf_ui.h> UF_initialize(); //获得当前图纸页 ...

  10. centos 根目录扩容

    添加一块磁盘 参考上一篇博文VMware Workstation 添加磁盘 挂载目录(centos) 查看当前磁盘挂载情况 [root@node1 ~]# fdisk -l Disk /dev/sda ...