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. CodeForces - 417B (思维题)

    Crash Time Limit: 1000MS   Memory Limit: 262144KB   64bit IO Format: %I64d & %I64u Submit Status ...

  2. QML入门教程

    QML是Qt推出的Qt Quick技术的一部分,是一种新增的简便易学的语言.QML是一种陈述性语言,用来描述一个程序的用户界面:无论是什么样子,以及它如何表现.在QML,一个用户界面被指定为具有属性的 ...

  3. Storm工程创建

    1.创建maven项目: pom.xml: <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi=&quo ...

  4. Start GitHub

    google 的各种被墙大家都懂的,所以转向GitHub 的怀抱. 以前其实也关注过,这回是下决心好好试用了. Fork repositories Forking creates a new, uni ...

  5. SQLServer2008 绑定默认值

    默认值或绑定:(newid())值:2E014A19-7AF5-471E-866C-DC5D6DFE59A5 (dateadd(day,(1),getdate()))值: 2014-07-31 20: ...

  6. error opening trace file: No such file or directory (2) ,can't load transform_config.xml

    出现这个错误:error opening trace file: No such file or directory (2) ,can't load transform_config.xml 是因为没 ...

  7. Ubuntu 11.10升级Ruby (1.8.7 --> 1.9.3或者其他任意版本)

    使用apt-get install ruby,安装的默认版本为1.8.7.想要使用更高版本,只能采用手工升级的方式. 方式1 使用RVM(推荐方式) 1 安装RVM http://rvm.io/rvm ...

  8. HealthKit开发教程之HealthKit的主要类型数据

    HealthKit开发教程之HealthKit的主要类型数据 在HealthKit中,我们将最常用到的数据称之为主要数据.主要数据基本上有三种:长度类型的数据.质量类型的数据.能量类型的数据.本节将主 ...

  9. Arduino可穿戴开发入门教程(大学霸内部资料)

    Arduino可穿戴开发入门教程(大学霸内部资料) 试读下载地址:链接:http://pan.baidu.com/s/1mg9To28 密码:z5v8 介绍:Arduino可穿戴开发入门教程(大学霸内 ...

  10. POJ1699 Best Sequence(AC自动机+状压DP)

    题目,求包含所有的给定的n个DNA片段的序列的最短长度. AC自动机上的DP题. dp[S][u]表示已经包含的DNA片段集合为S,且当前后缀状态是自动机第u个结点的最短长度 dp[0][0]=0 我 ...