记忆化搜索:HDU1078-FatMouse and Cheese(记忆化搜索)
FatMouse and Cheese
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2394 Accepted Submission(s): 913
Problem Description
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
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
1 2 5
10 11 6
12 12 7
-1 -1
#include<bits/stdc++.h>
using namespace std;
const int maxn = 110;
int maps[maxn][maxn];
int dp[maxn][maxn];
int n,k;
int dir[4][2] = {1,0,0,1,-1,0,0,-1};
bool check(int x,int y)
{
if(x<1 || y<1 || x>n || y>n)
return false;
else
return true;
}
int dfs(int x,int y)
{
if(dp[x][y])
return dp[x][y];
int r,c,ans = 0;
for(int i=0;i<4;i++)
for(int j=1;j<=k;j++)
{
r = x + dir[i][0]*j;
c = y + dir[i][1]*j;
if(check(r,c) && maps[r][c] > maps[x][y])
{
int temp = dfs(r,c);
if(temp > ans)
ans = temp;
}
}
dp[x][y] = ans + maps[x][y];
return dp[x][y];
}
int main()
{
while(scanf("%d%d",&n,&k) && n != -1 && k != -1)
{
memset(maps,0,sizeof(maps));
memset(dp,0,sizeof(dp)); for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
scanf("%d",&maps[i][j]); printf("%d\n",dfs(1,1));
}
}
记忆化搜索:HDU1078-FatMouse and Cheese(记忆化搜索)的更多相关文章
- HDU1078 FatMouse and Cheese 【内存搜索】
FatMouse and Cheese Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
- 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 ...
- kuangbin专题十二 HDU1078 FatMouse and Cheese )(dp + dfs 记忆化搜索)
FatMouse and Cheese Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
- 记忆化搜索,FatMouse and Cheese
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1107 http://acm.hdu.edu.cn/showpro ...
- (记忆化搜索) FatMouse and Cheese(hdu 1078)
题目大意: 给n*n地图,老鼠初始位置在(0,0),它每次行走要么横着走要么竖着走,每次最多可以走出k个单位长度,且落脚点的权值必须比上一个落脚点的权值大,求最终可以获得的最大权值 (题目很容 ...
- HDU1078 FatMouse and Cheese(DFS+DP) 2016-07-24 14:05 70人阅读 评论(0) 收藏
FatMouse and Cheese Problem Description FatMouse has stored some cheese in a city. The city can be c ...
- hdu1078 FatMouse and Cheese(记忆化搜索)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1078 题目大意: 题目中的k表示横向或者竖直最多可曾经进的距离,不可以拐弯.老鼠的出发点是(1,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 ...
- P - FatMouse and Cheese 记忆化搜索
FatMouse has stored some cheese in a city. The city can be considered as a square grid of dimension ...
随机推荐
- SpringBoot | 第十三章:测试相关(单元测试、性能测试)
前言 前面写了这么多章节,都是通过浏览器访问的形式,进行接口方法访问进而验证方法的正确与否.显然在服务或者接口比较少时,这么做没有啥问题,但一旦一个项目稍微复杂或者接口方法比较多时,这么验证就有点不符 ...
- 记ubuntu下安装Anaconda
晚上尝试在ubuntu 16.04版本下安装python的Anaconda3发行版. 从清华源下载的Anaconda3-Linux 64位版本安装包,然后顺利的下一步,下一步.....一切顺利!结果到 ...
- java使用线程请求访问每次间隔10分钟连续5次,之后停止请求
java使用线程请求访问每次间隔10分钟连续5次,收到相应的时候停止请求 package com.qlwb.business.util; /** * * * @类编号: * @类名称:RequestT ...
- php允许被跨域ajax请求
只要在被请求端,加一句: header('Access-Control-Allow-Origin: *');
- 【踩坑】服务器部署springboot应用时报错--端口被tomcat占用
今天将本机尬聊一下项目(基于netty-socketio)的服务端程序调试好以后,通过jar包部署在服务器的时候,出现了报错,提示tomcat已经占用了端口. 之前在部署iReview项目时的确是通过 ...
- angularjs嵌套路由
index.html <!DOCTYPE html> <html lang="en"> <head> <meta charset=&quo ...
- HTTPS与SSL(二)
CA Ca介绍 电子商务认证授权机构(CA, Certificate Authority),也称为电子商务认证中心,是负责发放和管理数字证书的权威机构,并作为电子商务交易中受信任的第三方,承担公钥体系 ...
- centos升级3.10内核到4.4
为了满足k8s对内核要求,默认Centos 7的内核为3.10建议升级到4.x内核 默认3.10内核升级为4.x内核 rpm -Uvh http://www.elrepo.org/elrepo-rel ...
- iOS界面设计切图小结
iOS界面设计切图小结 APR 12TH, 2013 1.基本尺寸 (1)界面 实际设计时按: iPhone4.4s:640px*960px iPhone5: 640px*1136px iPad:15 ...
- IOS NSBundle使用(访问文件夹)
NSBundle的相关信息 1.一个NSBundle代表一个文件夹,利用NSBundle能访问对应的文件夹 2.利用mainBundle就可以访问软件资源包中的任何资源 3.模拟器应用程序的安装路径: ...