HDU 1078 FatMouse and Cheese(记忆化搜索)
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
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.
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.
1 2 5
10 11 6
12 12 7
-1 -1
题目链接: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(记忆化搜索)的更多相关文章
- HDU - 1078 FatMouse and Cheese (记忆化搜索)
FatMouse has stored some cheese in a city. The city can be considered as a square grid of dimension ...
- HDU 1078 FatMouse and Cheese 记忆化搜索DP
直接爆搜肯定超时,除非你加了某种凡人不能想出来的剪枝...555 因为老鼠的路径上的点满足是递增的,所以满足一定的拓补关系,可以利用动态规划求解 但是复杂的拓补关系无法简单的用循环实现,所以直接采取记 ...
- HDU 1078 FatMouse and Cheese (记忆化搜索)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1078 老鼠初始时在n*n的矩阵的(0 , 0)位置,每次可以向垂直或水平的一个方向移动1到k格,每次移 ...
- HDU 1078 FatMouse and Cheese (记忆化搜索+dp)
详见代码 #include <iostream> #include <cstdio> #include <cstdlib> #include <memory. ...
- !HDU 1078 FatMouse and Cheese-dp-(记忆化搜索)
题意:有一个n*n的格子.每一个格子里有不同数量的食物,老鼠从(0,0)開始走.每次下一步仅仅能走到比当前格子食物多的格子.有水平和垂直四个方向,每一步最多走k格,求老鼠能吃到的最多的食物. 分析: ...
- hdu 1078 FatMouse and Cheese 记忆化dp
只能横向或竖向走...一次横着竖着最多k步...不能转弯的.... 为毛我的500+ms才跑出来... #include<cstdio> #include<iostream> ...
- HDU ACM 1078 FatMouse and Cheese 记忆化+DFS
题意:FatMouse在一个N*N方格上找吃的,每一个点(x,y)有一些吃的,FatMouse从(0,0)的出发去找吃的.每次最多走k步,他走过的位置能够吃掉吃的.保证吃的数量在0-100.规定他仅仅 ...
- hdu1078 FatMouse and Cheese(记忆化搜索)
转载请注明出处:http://blog.csdn.net/u012860063 题目链接:pid=1078" target="_blank">http://acm. ...
- hdu1078 FatMouse and Cheese —— 记忆化搜索
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1078 代码1: #include<stdio.h>//hdu 1078 记忆化搜索 #in ...
- P - FatMouse and Cheese 记忆化搜索
FatMouse has stored some cheese in a city. The city can be considered as a square grid of dimension ...
随机推荐
- C++复制构造函数和赋值符的区别
From http://blog.csdn.net/randyjiawenjie/article/details/6666937 非常感谢原作者分享. class CTest{public: CTe ...
- Android之UI控件
本文主要包括以下内容 Spinner的使用 Gallery的使用 Spinner的使用 Spinner的实现过程是 1. 在xml文件中定义Spinner的控件 2. 在activity中获取Spin ...
- [转]使用VC/MFC创建一个线程池
许多应用程序创建的线程花费了大量时间在睡眠状态来等待事件的发生.还有一些线程进入睡眠状态后定期被唤醒以轮询工作方式来改变或者更新状态信息.线程池可以让你更有效地使用线程,它为你的应用程序提供一个由系统 ...
- java 学习之路
一.基础篇 1.1 JVM 1.1.1. Java内存模型,Java内存管理,Java堆和栈,垃圾回收 http://www.jcp.org/en/jsr/detail?id=133 http://i ...
- 菜鸟学Linux命令:grep配合ls等使用
linux grep命令 (global search regular expression(RE) and print out the line )是一种强大的文本搜索工具,它能使用正则表达式搜索文 ...
- js 抽奖转盘实现
今天用js实现转盘抽奖功能,从后台返回的值可以固定转盘选择停止的任意位置 实现代码如下: js: <script> , i = ;//auto:时间对象 count:计数器 ,i : 计数 ...
- h5 canvas 小球移动
h5 canvas 小球移动 <!DOCTYPE html> <html lang="en"> <head> <meta charset= ...
- hdu 4022 STL
题意:给你n个敌人的坐标,再给你m个炸弹和爆炸方向,每个炸弹可以炸横排或竖排的敌人,问你每个炸弹能炸死多少个人. /* HDU 4022 G++ 1296ms */ #include<stdio ...
- ActiveMQ Exception: java.io.EOFException: Chunk stream does not exist
解决办法: 方法1. 去掉延迟功能:<broker xmlns="http://activemq.apache.org/schema/core " brokerName=&q ...
- BZOJ2783: [JLOI2012]树 dfs+set
2783: [JLOI2012]树 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 588 Solved: 347 Description 数列 提交文 ...