这是一道记忆化搜索,也就是有记录的搜索。

注意点:一次走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的更多相关文章

  1. HDU 1078 FatMouse and Cheese(记忆化搜索)

    FatMouse and Cheese Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe ...

  2. hdu 1078 FatMouse and Cheese (dfs+记忆化搜索)

    pid=1078">FatMouse and Cheese Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/ ...

  3. FatMouse and Cheese 动态化搜索

    FatMouse and Cheese Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe ...

  4. FatMouse and Cheese

    FatMouse and Cheese Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u ...

  5. (记忆化搜索) FatMouse and Cheese(hdu 1078)

    题目大意:   给n*n地图,老鼠初始位置在(0,0),它每次行走要么横着走要么竖着走,每次最多可以走出k个单位长度,且落脚点的权值必须比上一个落脚点的权值大,求最终可以获得的最大权值   (题目很容 ...

  6. 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 ...

  7. HDU 1078 FatMouse and Cheese ( DP, DFS)

    HDU 1078 FatMouse and Cheese ( DP, DFS) 题目大意 给定一个 n * n 的矩阵, 矩阵的每个格子里都有一个值. 每次水平或垂直可以走 [1, k] 步, 从 ( ...

  8. kuangbin专题十二 HDU1078 FatMouse and Cheese )(dp + dfs 记忆化搜索)

    FatMouse and Cheese Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe ...

  9. 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 ...

随机推荐

  1. mysql 服务不见了的解决办法

    昨天打开电脑mysql突然连接不了了,去服务里找,却找不到mysql服务了 解决:5.0版本:开始->运行->cmd,进到mysql安装的bin目录D:\MySQL\bin>mysq ...

  2. MVC 全局异常过滤器HandleErrorAttribute

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...

  3. oracle学习-安装卸载

  4. 使用phpExcel导入Excel进入Mysql数据库

    1.http://phpexcel.codeplex.com/下载phpExcel 2. <? require_once 'PHPExcel.php'; require_once 'PHPExc ...

  5. 在Win8上安装pyinstaller打包python成为可执行文件

    首先我使用的电脑系统是: Windows-8-6.2.9200 Python的版本是: 2.7.8 默认已安装python2.7且设置好了环境变量. 仅为个人记录,非教程. 首先先安装pip: 首先先 ...

  6. 变态最大值--nyoj题目811

    变态最大值 时间限制:1000 ms  |  内存限制:65535 KB 难度:1   描述 Yougth讲课的时候考察了一下求三个数最大值这个问题,没想到大家掌握的这么烂,幸好在他的帮助下大家算是解 ...

  7. Android上使用OpenglES2.0遇到的一点问题

    按照教程开发OpenglES2.0应用,遇到Logcat报错“Called unimplemented OpenGL ES API” 在论坛和stackoverflow上找到了答案. 1.manife ...

  8. 【转】Delphi内嵌ASM简易教程

    Delphi内嵌ASM简易教程 作者:heiying2006-03-19 18:33分类:默认分类标签: 前言 Delphi作为一个快速高效的开发平台,使用的人越来越多,但熟悉在Delphi代码中嵌入 ...

  9. UVA 1572 Self-Assembly

    拓扑排序,以边上标号为点,正方形为边,拓扑图中存在有向环时unbounded,否则bounded: 注意:仔细处理输入:   遍历一个点时,下一次遍历拼上的下一个方形边:即假设遍历到 A+ 时,下次从 ...

  10. Hadoop伪分布式搭建步骤

    说明: 搭建环境是VMware10下用的是Linux CENTOS 32位,Hadoop:hadoop-2.4.1  JAVA :jdk7 32位:本文是本人在网络上收集的HADOOP系列视频所附带的 ...