题意:FatMouse在一个N*N方格上找吃的,每一个点(x,y)有一些吃的,FatMouse从(0,0)的出发去找吃的。每次最多走k步,他走过的位置能够吃掉吃的。保证吃的数量在0-100。规定他仅仅能水平或者垂直走,每走一步。下一步吃的数量须要大于此刻所在位置,问FatMouse最多能够吃多少东西。

须要对步数进行扩展。

#include<iostream>
using namespace std; #define N 101
#define max(a,b) ((a)>(b)?(a):(b))
int dp[N][N],map[N][N];
int k,n;
int dir[4][2]={{1,0},{0,1},{-1,0},{0,-1}}; bool ok(int x,int y) //推断边界
{
return x>=0 && y>=0 && x<n && y<n;
} int dfs(int x,int y) //记忆化搜索
{
int i,j,max=0,xt,yt,tmp; if(dp[x][y]>0)
return dp[x][y];
for(i=0;i<4;i++)
for(j=1;j<=k;j++)
{
xt=dir[i][0]*j+x;
yt=dir[i][1]*j+y;
if(ok(xt,yt)&&map[x][y]<map[xt][yt])
{
tmp=dfs(xt,yt);
if(tmp>max) //找到最大的
max=tmp;
}
}
dp[x][y]=max+map[x][y];
return dp[x][y];
} int main()
{
int i,j; while(scanf("%d%d",&n,&k)==2 && k!=-1 && n!=-1)
{
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&map[i][j]);
memset(dp,0,sizeof(dp));
cout<<dfs(0,0)<<endl;
}
return 0;
}

HDU ACM 1078 FatMouse and Cheese 记忆化+DFS的更多相关文章

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

    FatMouse has stored some cheese in a city. The city can be considered as a square grid of dimension ...

  2. HDU 1078 FatMouse and Cheese 记忆化搜索DP

    直接爆搜肯定超时,除非你加了某种凡人不能想出来的剪枝...555 因为老鼠的路径上的点满足是递增的,所以满足一定的拓补关系,可以利用动态规划求解 但是复杂的拓补关系无法简单的用循环实现,所以直接采取记 ...

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

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1078 老鼠初始时在n*n的矩阵的(0 , 0)位置,每次可以向垂直或水平的一个方向移动1到k格,每次移 ...

  4. HDU 1078 FatMouse and Cheese (记忆化搜索+dp)

    详见代码 #include <iostream> #include <cstdio> #include <cstdlib> #include <memory. ...

  5. hdu 1078 FatMouse and Cheese 记忆化dp

    只能横向或竖向走...一次横着竖着最多k步...不能转弯的.... 为毛我的500+ms才跑出来... #include<cstdio> #include<iostream> ...

  6. !HDU 1078 FatMouse and Cheese-dp-(记忆化搜索)

    题意:有一个n*n的格子.每一个格子里有不同数量的食物,老鼠从(0,0)開始走.每次下一步仅仅能走到比当前格子食物多的格子.有水平和垂直四个方向,每一步最多走k格,求老鼠能吃到的最多的食物. 分析: ...

  7. hdu1078 FatMouse and Cheese(记忆化搜索)

    转载请注明出处:http://blog.csdn.net/u012860063 题目链接:pid=1078" target="_blank">http://acm. ...

  8. hdu1078 FatMouse and Cheese —— 记忆化搜索

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1078 代码1: #include<stdio.h>//hdu 1078 记忆化搜索 #in ...

  9. P - FatMouse and Cheese 记忆化搜索

    FatMouse has stored some cheese in a city. The city can be considered as a square grid of dimension ...

随机推荐

  1. oracle group by rollup,decode,grouping,nvl,nvl2,nullif,grouping_id,group_id,grouping sets,RATIO_TO

    干oracle 047文章12当问题,经验group by 声明.因此邂逅group by  rollup,decode,grouping,nvl,nvl2,nullif,RATIO_TO_REPOR ...

  2. LF will be replaced by CRLF问题解决方法

    [GIT] warning: LF will be replaced by CRLF问题解决方法 开发环境: 操作系统: windows xp ruby 1.9.2 rails 3.1.3 git v ...

  3. Cortex-mo指令集

    处理器使用的是ARMv6-M Thumb指令集,包括大量的32位的使用Thumb-2技术的指令.表7-22列出了Cortex-M0指令和它们的周期数.周期计数以零等待状态的系统为基准. 表7-22  ...

  4. Vi操作技巧

    Vi操作技巧: :nu    显示当前所在行的行号 :set nu    显示全部行号 :set nonu        取消显示行号 /字符串    查询字符串,按n查询下一个,按N查询上一个 持续 ...

  5. Linux命令 tar 打包解压缩

    --打包:tartar -cvf 命名文件.tar 要压缩的目录tar -xvf 要解压的包.tartar -tf  要查看的包.tartar -f 包.tar -r file             ...

  6. 自定义控件 环形进度条 ProgressBar

    使用 public class MainActivity extends Activity implements OnComompleteListener {     private int num  ...

  7. DataGrid( 数据表格) 组件[3]

    本节课重点了解 EasyUI 中 DataGrid(数据表格)组件的使用方法,这个组件依赖于Panel(面板).Resizeable(调整大小).LinkButton(按钮).Pageination( ...

  8. C#。5 函数 类

    函数:能够独立完成某个功能的模块.          好处:1.结构更清析(编写.维护方便 ).2.代码重用.3.分工开发.          四要素:名称,输入(参数),输出(返回的类型),加工(函 ...

  9. (转)ligerUI 使用教程之Tip介绍与使用

    概述:   ligertip是ligerUI系列插件中的tooltip类插件,作用是弹一个浮动层,起提示作用   阅读本文要求具备jQuery的基本知识,不然文中的javascript代码不易理解 截 ...

  10. js中substring和substr的用法 (转)

    1.substring 方法 定义和用法 substring 方法用于提取字符串中介于两个指定下标之间的字符. 语法 stringObject.substring(start,stop) 参数     ...