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

注意点:一次走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. Html5 Css实现方形图片 圆形显示

    <!doctype html><html><head><meta charset="utf-8"><title>方形图片 ...

  2. C++中vector和list排序

    容器.泛型算法.和类是不是就是C++相对于C"++"的那部分呢?暂时先这么认为吧.如果这篇博客有幸被别人看到,请帮忙指出.--C++ 菜鸟 留. vector的迭代器是随机访问迭代 ...

  3. TableLayout属性

    整理于http://naotu.baidu.com/file/e5880b84b1a906838116f7a45f58de78

  4. TCP/IP详解之:SNMP

    基于TCP/IP的网络管理包含3个组成部分: 一个管理信息库MIB:MIB包含所有代理进程的所有可被查询和修改的参数 关于MIB的一套公用的结构和表示符号,即SMI(管理信息结构) 管理进程和代理进程 ...

  5. vim自动补全文章搜集

    引用文章A:http://blog.csdn.net/wendy260310/article/details/18035555 文章介绍:添加C++标准库的tags文件方法.(中文版) 引用文章B:h ...

  6. *1022. D进制的A+B【考前最后一道题】

      /* *Main.c *1022. D进制的A+B Ver.1 *Created on : 2014.9.5 *****测试通过****** */ #include <stdio.h> ...

  7. Hibernate 体系结构简述

    SessionFactory: Hibernate的关键对象,它是单个数据库映射关系经过编译后的内存镜像,同时它是线程安全的.它是生成Session的工厂,本身需要依赖于ConnectionProvi ...

  8. C#调用C++DLL传递结构体数组的终极解决方案

    在项目开发时,要调用C++封装的DLL,普通的类型C#上一般都对应,只要用DllImport传入从DLL中引入函数就可以了.但是当传递的是结构体.结构体数组或者结构体指针的时候,就会发现C#上没有类型 ...

  9. JIRA官方:JIRA定制工作流

    定制适合项目的工作流 JIRA提供了一个缺省工作流和一系列问题类型,非常适合缺陷追踪和软件开发.在使用中你可以随时根据实际情况对流程进行调整,确保流程能够快速的适应坏境变化. 使你的工作流图形化 使用 ...

  10. Poj 1269 Intersecting Lines_几何模板

    #include <iostream> #include <math.h> #include <iomanip> #define eps 1e-8 #define ...