zoj 1107 FatMouse and Cheese(记忆化搜索)
题目链接:点击链接
题目大意:老鼠从(0,0)出发,每次在同一个方向上最多前进k步,且每次到达的位置上的数字都要比上一个位置上的数字大,求老鼠经过的位置上的数字的和的最大值
#include<stdio.h>
#include<string.h>
#define max(a,b) a>b?a:b
int n;
int k;//前进的步数
int map[105][105];
int ans[105][105];//记忆化搜索,保存中间搜索结果
int search(int x,int y)
{
int dx,dy;//要去的下一个位置
int i,maxx = 0;
if(ans[x][y] != -1) return ans[x][y];//已经搜索过,直接返回结果
for(i = 1 ; i <= k ; i ++)//向前走的步数
{
dx = x - i;//向上
if(dx >= 0 && dx < n && map[dx][y] > map[x][y])
{
ans[dx][y] = search(dx,y);
maxx = max(maxx,ans[dx][y]);
}
dx = x + i;//向下
if(dx >= 0 && dx < n && map[dx][y] > map[x][y])
{
ans[dx][y] = search(dx,y);
maxx = max(maxx,ans[dx][y]);
}
dy = y - i;//向左
if(dy >= 0 && dy < n && map[x][dy] > map[x][y])
{
ans[x][dy] = search(x,dy);
maxx = max(maxx,ans[x][dy]);
}
dy = y + i;//向右
if(dy >= 0 && dy < n && map[x][dy] > map[x][y])
{
ans[x][dy] = search(x,dy);
maxx = max(maxx,ans[x][dy]);
}
}
return maxx + map[x][y];
}
int main()
{
int i,j;
while(scanf("%d%d",&n,&k) && (n != -1 && k != -1))
{
for(i = 0 ; i < n ; i ++)
for(j = 0 ; j < n ; j ++)
scanf("%d",&map[i][j]);
memset(ans,-1,sizeof(ans));
printf("%d\n",search(0,0));
}
return 0;
}
zoj 1107 FatMouse and Cheese(记忆化搜索)的更多相关文章
- HDU - 1078 FatMouse and Cheese (记忆化搜索)
FatMouse has stored some cheese in a city. The city can be considered as a square grid of dimension ...
- P - FatMouse and Cheese 记忆化搜索
FatMouse has stored some cheese in a city. The city can be considered as a square grid of dimension ...
- hdu1078 FatMouse and Cheese(记忆化搜索)
转载请注明出处:http://blog.csdn.net/u012860063 题目链接:pid=1078" target="_blank">http://acm. ...
- HDU 1078 FatMouse and Cheese 记忆化搜索DP
直接爆搜肯定超时,除非你加了某种凡人不能想出来的剪枝...555 因为老鼠的路径上的点满足是递增的,所以满足一定的拓补关系,可以利用动态规划求解 但是复杂的拓补关系无法简单的用循环实现,所以直接采取记 ...
- hdu1078 FatMouse and Cheese —— 记忆化搜索
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1078 代码1: #include<stdio.h>//hdu 1078 记忆化搜索 #in ...
- [HDOJ1078]FatMouse and Cheese(记忆化搜索)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1078 题意:给出n, k,然后给出n*n的地图,(下标0~n-1),有一只老鼠从(0,0)处出发,只能 ...
- HDU 1078 FatMouse and Cheese (记忆化搜索)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1078 老鼠初始时在n*n的矩阵的(0 , 0)位置,每次可以向垂直或水平的一个方向移动1到k格,每次移 ...
- HDU 1078 FatMouse and Cheese (记忆化搜索+dp)
详见代码 #include <iostream> #include <cstdio> #include <cstdlib> #include <memory. ...
- HDU ACM 1078 FatMouse and Cheese 记忆化+DFS
题意:FatMouse在一个N*N方格上找吃的,每一个点(x,y)有一些吃的,FatMouse从(0,0)的出发去找吃的.每次最多走k步,他走过的位置能够吃掉吃的.保证吃的数量在0-100.规定他仅仅 ...
随机推荐
- 基于visual Studio2013解决C语言竞赛题之0601判断素数函数
题目 解决代码及点评 //编写一函数判断一个数是否为素数 #include<stdio.h> #include <stdlib.h> # ...
- source insight 中文注释为乱码解决
1. source insight 中文注释为乱码解决 http://blog.csdn.net/bingfeng1210/article/details/7527059 2. Source Insi ...
- 工作随记 warning: 'includeantruntime' was not set, defaulting to build.sysclasspath=last; set to false for repeatable builds
错误信息:F:\BUILD\IDS7020\trunk\manage_src\dev\java_src\tds7030-web\Ant\build.xml:344: warning: 'include ...
- 安装MyEclipse Color Themes
下载地址:http://eclipsecolorthemes.org/?list=toppicks&lang=html 安装步骤如下: 1.Import---Preferences 2.选择下 ...
- python package的概念
主目录下面文件夹pack,可以按照 import pack.mo as mo 来调用模块,意为把pack文件夹当作一个包
- NoSQL简要数据库
前言 NoSQL:not only SQL(不No SQL啊),它的意思是:在关系数据库中使用关系数据库时适用,但在关系数据库中不适合本地使用其它数据库.NoSQL了弥补关系型数据库的不足,能够算是关 ...
- Java7新特性(一)Coin
1.语法糖 数字下划线 2.switch语句中的String 3.multicatch 4.final重抛 对比上份代码 5.try-with-resources(TWR) AutoC ...
- 三种java 去掉字符串中的重复字符函数
三种java 去掉字符串中的重复字符函数 public static void main(string[] args) { system.out.println(removerepeatedchar( ...
- 基于visual Studio2013解决面试题之1105字符串压缩
题目
- UPC 2959: Caoshen like math 这就是个水题
http://acm.upc.edu.cn/problem.php?id=2959 这就是个水题,之所以要写这个题是感觉很有纪念意义 用力看就是盲……23333333333333333 这个题就是最小 ...