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

注意点:一次走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. css中bug记录

    1.margin塌陷,通俗叫法. 假如一个盒子box里边嵌套了两个盒子(记为box1,box2).box1的margin-top不会如预想的一样在box顶部撑开一个边距,而是以包含box的容器上边界为 ...

  2. cover letter issues

    All cover letters should: Explain why you are sending a resume. Don't send a resume without a cover ...

  3. C#常用的内置委托

    using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threa ...

  4. STM32的例程GPIO的汇编指令初探

    任务一:寻找main函数的汇编指令集 任务二:寻找main函数中的SystemClock_Config函数的汇编指令集 寻找main函数的汇编指令集 运行例程中GPIO工程时,总会加载startup_ ...

  5. java中静态代码块的用法 static用法详解

    (一)java 静态代码块 静态方法区别一般情况下,如果有些代码必须在项目启动的时候就执行的时候,需要使用静态代码块,这种代码是主动执行的;需要在项目启动的时候就初始化,在不创建对象的情况下,其他程序 ...

  6. 解决ERROR C2011: 'FD_SET' : 'STRUCT' TYPE REDEFINITION问题

    在socket编程的过程中头文件中 #include <windows.h> #include "stdafx.h" #include "WinSock2.h ...

  7. TIANKENG’s restaurant--hdu4883

    TIANKENG’s restaurant Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/O ...

  8. Elevator(hdoj 1008)

    Problem Description The highest building in our city has only one elevator. A request list is made u ...

  9. 发现中文版《C Primer Plus第五版》示例程序的一个错误

    错误的程序出现再第17章的499页ListItemCount()和500页的Traverse()两个函数上. 原著包含所有函数定义的list.c如下: #include<stdio.h> ...

  10. 使用StackTrace堆栈跟踪记录详细日志(可获取行号)

    上一篇我们提到使用.NET自带的TraceSource实现简单的日志,具体请看<轻松背后的N+疲惫——系统日志>,这一篇注意想讲的是日志的详细记录,包含请求开始到结束的过程中调用的方法链以 ...