hdu 1078 FatMouse and Cheese(记忆搜)
N*N的矩阵,每个格子上有一个值。
老鼠起始在(1,1),每次只能水平着走或垂直着走。且最多只能走K步。且走到的格子里的值必须比上一次呆的格子里的值大。
问老鼠最多收集到多少值。
思路:
记忆搜好写、方便。
注意边界
代码:
int n,k;
int a[105][105];
int dp[105][105]; int dfs(int x,int y){
if(dp[x][y]>0)
return dp[x][y];
for(int i=x+1;i<=min(n,x+k);++i){
if(a[i][y]>a[x][y]){
dp[x][y]=max( dp[x][y],dfs(i,y)+a[x][y] );
}
}
for(int i=x-1;i>=max(1,x-k);--i){
if(a[i][y]>a[x][y]){
dp[x][y]=max( dp[x][y],dfs(i,y)+a[x][y] );
}
}
for(int i=y+1;i<=min(n,y+k);++i){
if(a[x][i]>a[x][y]){
dp[x][y]=max( dp[x][y],dfs(x,i)+a[x][y] );
}
}
for(int i=y-1;i>=max(1,y-k);--i){
if(a[x][i]>a[x][y]){
dp[x][y]=max( dp[x][y],dfs(x,i)+a[x][y] );
}
}
if(dp[x][y]==0)
dp[x][y]=a[x][y];
return dp[x][y];
} int main(){ while(scanf("%d%d",&n,&k)!=EOF){
if(n==-1 && k==-1){
break;
}
rep(i,1,n){
rep(j,1,n){
scanf("%d",&a[i][j]);
}
}
mem(dp,0);
int ans=dfs(1,1);
printf("%d\n",ans);
} 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 (记忆化搜索)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1078 老鼠初始时在n*n的矩阵的(0 , 0)位置,每次可以向垂直或水平的一个方向移动1到k格,每次移 ...
- HDU 1078 FatMouse and Cheese 记忆化搜索DP
直接爆搜肯定超时,除非你加了某种凡人不能想出来的剪枝...555 因为老鼠的路径上的点满足是递增的,所以满足一定的拓补关系,可以利用动态规划求解 但是复杂的拓补关系无法简单的用循环实现,所以直接采取记 ...
- HDU 1078 FatMouse and Cheese (记忆化搜索+dp)
详见代码 #include <iostream> #include <cstdio> #include <cstdlib> #include <memory. ...
- hdu 1078 FatMouse and Cheese 记忆化dp
只能横向或竖向走...一次横着竖着最多k步...不能转弯的.... 为毛我的500+ms才跑出来... #include<cstdio> #include<iostream> ...
- HDU 1078 FatMouse and Cheese ( DP, DFS)
HDU 1078 FatMouse and Cheese ( DP, DFS) 题目大意 给定一个 n * n 的矩阵, 矩阵的每个格子里都有一个值. 每次水平或垂直可以走 [1, k] 步, 从 ( ...
- hdu 1078 FatMouse and Cheese (dfs+记忆化搜索)
pid=1078">FatMouse and Cheese Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/ ...
- HDU 1078 FatMouse and Cheese(记忆化搜索)
FatMouse and Cheese Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
- HDU - 1078 FatMouse and Cheese(记忆化+dfs)
FatMouse and Cheese FatMouse has stored some cheese in a city. The city can be considered as a squar ...
随机推荐
- HDU1213How Many Tables(基础并查集)
HDU1213How Many Tables Problem Description Today is Ignatius' birthday. He invites a lot of friends. ...
- Linux安装Cockpit监控服务
CentOS/RHEL 8的新特性之一就是自带了一个cockpit的监控服务.通过c/s架构模式运行,客户端输入ip:端口即可访问 这类似于glances监控. 如果你不是使用的centos/rhel ...
- jmeter调度器的使用
前言 使用jmeter 做压测的时候,希望对一个接口持续压测 10 分钟或者半小时,可以使用调度器设置持续压测时间. https://www.cnblogs.com/yoyoketang/p/1415 ...
- P6295-有标号 DAG 计数【多项式求逆,多项式ln】
正题 题目链接:https://www.luogu.com.cn/problem/P6295 题目大意 求所有\(n\)个点的弱联通\(DAG\)数量. \(1\leq n\leq 10^5\) 解题 ...
- sonar入门
一.Sonar是什么? 根据我的了解,可以说Sonar包含三个部分: SonarQube是一种自动代码审查工具,用于检测代码中的错误,漏洞和代码味道.它可以与您现有的工作流程集成,以实现跨项目分支和提 ...
- 11.2.0.4 RAC manual opatch
1.Stop the CRS managed resources running from DB homes. If this is a GI Home environment, as the dat ...
- 数据库MySQL主从-GTID
1.第一步在主服务器上/etc/my.cnf/下添加 log-bin=log-bin server-id=1 gtid_mode=ON enforce_gtid_consistency 2.第二步:重 ...
- 『Mivik的萌新赛 & Chino的比赛 2020』T2 题解 Galgame
如果这是我最后一篇题解,请每年为我上坟. Galgame 题目传送门 Decription as_lky 搞到了很多 Galgame(真的很多!).一款 Galgame 可以被描述为很多场景(Scen ...
- CF125E MST company (凸优化+MST)
qwq自闭的一个题 我来修锅辣!!!!!! 这篇题解!可以\(hack\)全网大部分的做法!!! 首先,我们可以把原图中的边,分成两类,一类是与\(1\)相连,另一类是不与\(1\)相连. 原题就转化 ...
- Redis 面试常见问题———缓存雪崩、缓存击穿以及缓存穿透
在开发中会面临缓存异常可能会出现三个问题,分别是缓存雪崩.缓存击穿和缓存穿透.这三个问题会导致大量请求从缓存转移到数据库,如果请求的并发量很大的话,就会导致数据库崩溃.所以在面试官也会经常问这些问题. ...