zoj1107 FatMouse and Cheese
这是一道记忆化搜索,也就是有记录的搜索。
注意点:一次走k步不能拐弯
int bfs(int x,int y)
{
int mm=0;
if(ans[x][y]>=0)
return ans[x][y];
for(int i=0;i<4;i++)
{
for(int j=1;j<=k;j++)
{
int tx=x+j*dx[i];
int ty=y+j*dy[i];
if(!in(tx,ty)||g[tx][ty]<=g[x][y])
continue;
int res=bfs(tx,ty);
mm=max(res,mm);
}
}
return ans[x][y]=mm+g[x][y];
}
递归返回,这个写法要多多注意。
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<map>
#include<iostream>
#include<cstring>
#include<queue>
#include<vector>
using namespace std;
#define LL long long
int n,k;
int g[105][105];
int ans[105][105];
int dx[4]={-1,1,0,0};
int dy[4]={0,0,-1,1};
bool in(int x,int y)
{
if(x>=0&&x<n&&y>=0&&y<n)
return 1;
return 0;
}
int bfs(int x,int y)
{
int mm=0;
if(ans[x][y]>=0)
return ans[x][y];
for(int i=0;i<4;i++)
{
for(int j=1;j<=k;j++)
{
int tx=x+j*dx[i];
int ty=y+j*dy[i];
if(!in(tx,ty)||g[tx][ty]<=g[x][y])
continue;
int res=bfs(tx,ty);
mm=max(res,mm);
}
}
return ans[x][y]=mm+g[x][y];
}
int main()
{
//freopen("input.txt","r",stdin);
while(scanf("%d%d",&n,&k)==2)
{
if(n==-1)
break;
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
{
scanf("%d",&g[i][j]);
}
memset(ans,-1,sizeof(ans));
int f=bfs(0,0);
printf("%d\n",f);
}
}
zoj1107 FatMouse and Cheese的更多相关文章
- 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+记忆化搜索)
pid=1078">FatMouse and Cheese Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/ ...
- FatMouse and Cheese 动态化搜索
FatMouse and Cheese Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
- FatMouse and Cheese
FatMouse and Cheese Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u ...
- (记忆化搜索) 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 ...
- HDU 1078 FatMouse and Cheese ( DP, DFS)
HDU 1078 FatMouse and Cheese ( DP, DFS) 题目大意 给定一个 n * n 的矩阵, 矩阵的每个格子里都有一个值. 每次水平或垂直可以走 [1, k] 步, 从 ( ...
- kuangbin专题十二 HDU1078 FatMouse and Cheese )(dp + dfs 记忆化搜索)
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 ...
随机推荐
- 线程:Java主线程等待子线程结束
使用Thread.join()方法: public class App { public static void main(String[] args) { testMain(); } public ...
- ExtJS4.x动态加载js文件
动态加载js文件是ext4.x的一个新特性,可以有效的减少浏览器的压力,提高渲染速度.如动态加载自定义组件 1.在js/extjs/ux目录下,建立自定义组件的js文件. 2.编写MyWindow.j ...
- C++在数组元素个数未知情况下声明数组
我们都从书上学习的方法,定义一个数组需要数组名.类型以及数组元素个数,一般定义必须明确元素的个数,否则无法通过编译. 1. int a[]; 2. int n; int a[n]; 就想上面这两种情况 ...
- Java路径问题最终解决方案—可定位所有资源的相对路径寻址
1.在Java项目中,应该通过绝对路径访问文件,以下为访问的常用方法: 第一种方法:类名.class.getResource("/").getPath()+文件名 第二种方法:Th ...
- Python闭包及装饰器
Python闭包 先看一个例子: def outer(x): def inner(y): return x+y return innder add = outer(8) print add(6) 我们 ...
- 手机触摸屏的JS事件
处理Touch事件能让你跟踪用户的每一根手指的位置.你可以绑定以下四种Touch事件: touchstart: // 手指放到屏幕上的时候触发 touchmove: // 手指在屏幕上移动的时候触发 ...
- 如何看linux是32位还是64位
查看linux是多少位的几位方法: 查看linux机器是32位还是64位的方法: 方法一: file /sbin/init 或者 file /bin/ls 结果如下:/sbin/init: ELF ...
- HDU 2553 N皇后问题(dfs)
N皇后问题 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Description 在 ...
- SQL Server 解决CPU 高占用的一般step
第一步: 看这些CPU是不是SQL Server用的. 第二步: 确定SQL Server 有没有引发17883\17884错误 第三步: 找出使用CPU最高的语句进行优化.(sys.dm_exec_ ...
- 山寨QQ音乐的布局(一)
学了两天IOS趁着还没忘光,巩固一下所学知识想做点东西,由于自己的设计能力有限,所以就山寨一下吧,说到山寨怎么能忘了腾讯呢,今天发现QQ音乐的设计风格是扁平化的,小清新风格,所以就山寨一下它吧.. 由 ...