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. Tools: CGAL 安装指南

    环境:Microsoft Win7 SP1 Visual Studio 2015 SP3 参考: https://blog.csdn.net/milkpku/article/details/50241 ...

  2. ubuntu 16.04 pecl 不能安裝 mcrypt

    vagrant@ubuntu-xenial:/etc/apt$ sudo pecl install mcrypt-1.0.1 downloading mcrypt-1.0.1.tgz ... Star ...

  3. 进程、线程、协程、CPU

    进程.线程.CPU 进程是计算机中的程序关于某数据集合上的一次运行活动,是系统进行资源分配和调度的基本单位,是操作系统结构的基础.或者说进程是具有一定独立功能的程序关于某个数据集合上的一次运行活动,进 ...

  4. bat命令自用其(一)

    每秒钟打印ping命令结果到指定文件: @echo off set /p ip=Input the IP required to monitor: :starts echo %date% %time% ...

  5. python入门 集合(四)

    集合 集合是一个无序的不重复元素序列,可以迭代,也可以修改.集合迭代的时候元素是随机的. 集合通常用来 membership testing, 去重, 也可以用来求交集并集补集. 介绍一下如何创建集合 ...

  6. 快速排序--Python实现

    快速排序算法:1.选择一个基准数2.小于基准数的放左边,大于基准数的放右边3.利用递归的方法针对左边的数据进行快速排序,再对右边的数据进行快速排序4.递归停止的条件:数组为空或者只有一个元素 时间复杂 ...

  7. DLL劫持技术例子: HijackDll

    控制台程序:DllLoader Dll加载器,用于动态加载目标Dll,并动态调用目标函数 #include <cstdio> #include <windows.h> type ...

  8. mysql中的Date日期格式的问题:只有日期没有时间及格式化时间

    只有日期没有时间,把xml中的date改为timestamp 格式化最简单的方法:@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", time ...

  9. CometOJ Contest #3 C

    题目链接:https://cometoj.com/contest/38/problem/C?problem_id=1542&myself=0&result=0&page=1&a ...

  10. expect安装

    expect是在tcl基础上创建起来的,因此在安装expect之前需要安装tcl 安装TCL下载地址:http://www.tcl.tk/software/tcltk/download.html[ro ...