FatMouse and Cheese

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 8610    Accepted Submission(s): 3611

Problem Description
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
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
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
 
Sample Output
37
 

题目链接:HDU 1078

题目中的k是指在当前位置可以最多直接从(x,y)跳到(x+k*dirx,y+k*diry),不是指只能总体上在直线上平移多少距离,比如一条直线3 2 4,若以3为起点当k=2时就可以直接从3跳到4,不然由于无法到小于当前格子食物数量的位置就只能是3了,跳过去就可以得到3+4=7的食物量。然后这题跟滑雪又不一样,滑雪是可以从任何一个位置为起点,这题只能从(0,0)开始,因此最后不是取max(样例很巧合地取max也是37,WA N次)而是直接输出dp[0][0]

代码:

#Include<stdio.h>
#include<bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define CLR(x,y) memset(x,y,sizeof(x))
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
typedef pair<int,int> pii;
typedef long long LL;
const double PI=acos(-1.0);
const int N=110;
int pos[N][N],dir[4][2]={{0,1},{0,-1},{1,0},{-1,0}};
int dp[N][N];
int n,k;
inline bool check(int x,int y)
{
return (x>=0&&x<n&&y>=0&&y<n);
}
int dfs(int x,int y)
{
if(dp[x][y])
return dp[x][y];
else
{
int maxm=0;
for (int s=1; s<=k; ++s)
{
for (int i=0; i<4; ++i)
{
int xx=x+s*dir[i][0];
int yy=y+s*dir[i][1];
if(check(xx,yy)&&pos[xx][yy]>pos[x][y])
maxm=max<int>(maxm,dfs(xx,yy));
}
}
return dp[x][y]=maxm+pos[x][y];
}
}
int main(void)
{
int i,j;
while (~scanf("%d%d",&n,&k)&&(n!=-1&&k!=-1))
{
CLR(dp,0);
for (i=0; i<n; ++i)
for (j=0; j<n; ++j)
scanf("%d",&pos[i][j]);
for (i=0; i<n; ++i)
for (j=0; j<n; ++j)
dfs(i,j);
printf("%d\n",dp[0][0]);
}
return 0;
}

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

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

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

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

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

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

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

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

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

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

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

  6. hdu 1078 FatMouse and Cheese 记忆化dp

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

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

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

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

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

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

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

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

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

随机推荐

  1. 打开genesis时一直在等待,后出现Timeout in communication read解决方法

    运行输入:netsh winsock reset 然后重启电脑

  2. 关于struts 2中的日期问题

    struts 2中引入了大量的jquery的内容 其中日期问题总结一下: 步骤: 1.当然不用说,先建一个web项目 2.导入struts2所需要的jar包,以及此插件的包(当然你也可以用:strut ...

  3. hdu 1012:u Calculate e(数学题,水题)

    u Calculate e Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  4. Java Hour 54 Spring Framework 1

    总之,Srping Framework 很好很强大. 1 Spring Framework 介绍 省下你和transcation APIs, JMX APIs, JMS APIs 交流的功夫. 1.1 ...

  5. myeclipse打war包

    转自:http://wjlvivid.iteye.com/blog/1401707 右键选中项目,选择export然后选择J2EE->WAR File,点击next 接下来指定war包的存放路径 ...

  6. android操作XML的几种方式(转)

    XML作为一种业界公认的数据交换格式,在各个平台与语言之上,都有广泛使用和实现.其标准型,可靠性,安全性......毋庸置疑.在android平台上,我们要想实现数据存储和数据交换,经常会使用到xml ...

  7. ScrollView与ListView的冲突

    众所周知ListView与ScrollView都具有滚动能力,对于这样的View控件,当ScrollView与ListView相互嵌套会成为一种问题: 问题一:ScrollView与ListView嵌 ...

  8. exec命令

    exec 命令实例 find . -name "*.cc" -exec grep -P -n -H --color=auto "[^\w]main[^\w]" ...

  9. android 音频编解码1

    1. Android 官方的 MediaCodec API 该 API 是在 Andorid 4.1 (API 16) 版本引入的 MediaCodec 使用的基本流程是: 1234567891011 ...

  10. js:语言精髓笔记4----面向对象概要与运算符二义性

    实例创建:obj = new contructor[(arguments)]; //如果没有参数可以忽略括号:所以注意这不是函数调用: 直接量与初始器:在之前的基本表达式中将直接量与初始器分开,这时因 ...