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

注意点:一次走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. 数据结构算法应用C++语言描述——(1)C++基础知识

    一.二维数组 (1)二维数组的列是固定时,行未知时使用动态分配 当一个二维数组每一维的大小在编译时都是已知时,可以采用类似于创建一维数组的语法 来创建二维数组.例如,一个类型为char的7×5数组可用 ...

  2. 如何写一个数据库How do you build a database?(转载)

    转载自:http://www.reddit.com/r/Database/comments/27u6dy/how_do_you_build_a_database/ciggal8 Its a great ...

  3. Log4j学习手记

    注:今天难得清闲,学了下log4j,参考博文 http://www.cnblogs.com/pony/archive/2008/08/25/1275484.html ,文字部分基本上来自该博文,我只是 ...

  4. 新浪微博iOS示例,登录,获取个人信息

    1.导入第三方库和系统框架

  5. CSS圆角样式

    CSS圆角: /*纯css,设置图片圆角*/ #top2 { margin-left:20px; padding:10px; width:600px; height:300px; border: 5p ...

  6. 关于淘宝的数据来源,针对做淘宝客网站的淘宝api调用方法

    上次写了个淘宝返利模式的博客,直接被移除首页,不知道何故啊.可能是真的跟技术不太刮边. 众所周知,能够支撑一个网站运营的最基础不是程序写的多么好.也不是有多么牛X的运营人员,最主要的是数据,如果没有数 ...

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

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

  8. c++中在一个类中定义另一个只有带参数构造函数的类的对象

    c++中在一个类中定义另一个只有带参数构造函数的类的对象,编译通不过 #include<iostream> using namespace std; class A { public:  ...

  9. struts2_20140720

    有这样的感觉:前面学的东西弄会了,过了一段时间又感觉陌生了,还要重新开始.这次想个好办法,把写的程序用博客记录下来,把自己的学历历程用博客的形式呈现出来,一来可以方便复习,而来可以以后开发程序可以快速 ...

  10. iostat,mpstat,sar即时查看工具,sar累计查看工具

    iostat,mpstat,sar即时查看工具,sar累计查看工具