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. Vultr账号被锁定的几个常见原因

    很多小伙伴使用都在问Vultr账号为什么又是会出现被锁定的情况,今天我们就来了解一下Vultr账号被锁定的几个常见原因. 1.Vultr主机违规使用 按照Vultr主机商的要求,我们的Vultr主机也 ...

  2. loj2000[SDOI2017]数字表格

    题意:f为Fibnacci数列.求$\prod_{1<=i<=n,1<=j<=m} f[gcd(i,j)]$. n,m<=1e6. 标程: #include<bit ...

  3. 使用multiprocessing模块创建进程

    #_author:来童星#date:2019/12/17from multiprocessing import Processimport timeimport os#两个子进程将会调用的两个方法de ...

  4. Android NDK 环境变量配置

    NDK_ROOT = C:\__S_D_K__\AndroidNDK\android-ndk-r20 在path 中加入  %NDK_ROOT% 我的路径在C盘 //个别的程序可能需要 NDK_ROO ...

  5. CSS——标签显示模式(display)

    非洲黑人: 皮肤内黑色素含量高,以吸收阳光中的紫外线,保护皮肤内部结构免遭损害,头发象羊毛一样卷曲,使每根卷发周围都有许多空隙,空隙充满空气,卷发有隔热作用. 欧洲白人: 生活寒带或着是说常年温度较低 ...

  6. c# ToString()格式大全(转)

    stringstr1 =string.Format("{0:N1}",56789);               //result: 56,789.0stringstr2 =str ...

  7. 剑指offer——18打印从1到最大的n位数

    题目: 输入数字n,按顺序打印出从1到最大的n位十进制数.比如输入3,则打印出1.2.3一直到最大的3位数999. 题解: 注意大数溢出问题,故使用字符串更靠谱 class Solution { pu ...

  8. C#面向对象通信

    面向对象通信编程: 看起来像是调用本地的函数,就得到了结果: 实际上参数是传递到了远程机器上了,而函数也是在远程机器上运行的.

  9. spark-submit 应用程序第三方jar文件

    第一种方式:打包到jar应用程序 操作:将第三方jar文件打包到最终形成的spark应用程序jar文件中 应用场景:第三方jar文件比较小,应用的地方比较少 第二种方式:spark-submit 参数 ...

  10. USACO 2011 February Silver Cow Line /// 康拓展开模板题 oj22713

    题目大意: 输入n k,1-n的排列,k次操作 操作P:输入一个m 输出第m个排列 操作Q:输入一个排列 输出它是第几个排列 Sample Input 5 2P3Q1 2 5 3 4 Sample O ...