hdu1078 FatMouse and Cheese(记忆化搜索)
转载请注明出处:http://blog.csdn.net/u012860063
题目链接: pid=1078" target="_blank">http://acm.hdu.edu.cn/showproblem.php?pid=1078
blocks of cheese in a hole. Now he's going to enjoy his favorite food.
FatMouse begins by standing at location (0,0). He eats up the cheese where he stands and then runs either horizontally or vertically to another location. The problem is that there is a super Cat named Top Killer sitting near his hole, so each time he can run
at most k locations to get into the hole before being caught by Top Killer. What is worse -- after eating up the cheese at one location, FatMouse gets fatter. So in order to gain enough energy for his next run, he has to run to a location which have more blocks
of cheese than those that were at the current hole.
Given n, k, and the number of blocks of cheese at each grid location, compute the maximum amount of cheese FatMouse can eat before being unable to move.
a line containing two integers between 1 and 100: n and k
n lines, each with n numbers: the first line contains the number of blocks of cheese at locations (0,0) (0,1) ... (0,n-1); the next line contains the number of blocks of cheese at locations (1,0), (1,1), ... (1,n-1), and so on.
The input ends with a pair of -1's.
3 1
1 2 5
10 11 6
12 12 7
-1 -1
37
题目大意:
有一个地图。上面有正整数值,然后要求从(0, 0)点出发,每次仅仅能走向比当前点值更大的点,而且每次能够向四个方向走最多k步,要求走到最后所能得到的最大值。
解题思路:
因为题目要求有四个方向能够走。所以想到搜索,而要求走得最优解。所以能够用记忆化搜索。动态规划基础题,只是相当经典。
代码例如以下:(93ms)
Run ID | Submit Time | Judge Status | Pro.ID | Exe.Time | Exe.Memory | Code Len. | Language | Author |
10907677 | 2014-06-26 10:55:49 | Accepted | 1078 | 93MS | 392K | 910 B | G++ | 天资4747tym |
#include <cstdio>
#include <cstring> int dp[147][147],map[147][147];
int n,k;
int xx[4]={0,0,1,-1};
int yy[4]={1,-1,0,0}; int DFS(int x,int y)
{
int ans = 0,MAX = 0;
if(!dp[x][y])
{
for(int i = 1; i <= k; i++)
{
for(int j = 0; j < 4; j++)
{
int dx = x+xx[j]*i;
int dy = y+yy[j]*i;
if(dx>=0 && dx<n && dy>=0 && dy<n && map[x][y]<map[dx][dy])
{
ans = DFS(dx,dy);//每条路径能吃到的奶酪量
if(ans > MAX)
MAX = ans;
}
}
dp[x][y] = MAX+map[x][y];//从dp[x][y]開始能吃到的最大奶酪量
}
}
return dp[x][y];
} int main()
{
while(~scanf("%d%d",&n,&k))
{
if(n == -1 && k == -1)
break;
memset(dp,0,sizeof(dp));
memset(map,0,sizeof(map));
for(int i = 0; i < n; i++)
{
for(int j = 0; j < n; j++)
{
scanf("%d",&map[i][j]);
}
}
int TT = DFS(0,0);
printf("%d\n",TT);
}
return 0;
}
hdu1078 FatMouse and Cheese(记忆化搜索)的更多相关文章
- hdu1078 FatMouse and Cheese —— 记忆化搜索
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1078 代码1: #include<stdio.h>//hdu 1078 记忆化搜索 #in ...
- 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 ...
- HDU 1078 FatMouse and Cheese 记忆化搜索DP
直接爆搜肯定超时,除非你加了某种凡人不能想出来的剪枝...555 因为老鼠的路径上的点满足是递增的,所以满足一定的拓补关系,可以利用动态规划求解 但是复杂的拓补关系无法简单的用循环实现,所以直接采取记 ...
- [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.规定他仅仅 ...
- !HDU 1078 FatMouse and Cheese-dp-(记忆化搜索)
题意:有一个n*n的格子.每一个格子里有不同数量的食物,老鼠从(0,0)開始走.每次下一步仅仅能走到比当前格子食物多的格子.有水平和垂直四个方向,每一步最多走k格,求老鼠能吃到的最多的食物. 分析: ...
随机推荐
- [BZOJ3670] [NOI2014] 动物园 解题报告 (KMP)
题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=3670 Description 近日,园长发现动物园中好吃懒做的动物越来越多了.例如企鹅, ...
- sicily 1004. 简单哈希
Description 使用线性探测法(Linear Probing)可以解决哈希中的冲突问题,其基本思想是:设哈希函数为h(key) = d, 并且假定哈希的存储结构是循环数组, 则当冲突发生时, ...
- idea报错。Error:Failed to load project configuration: cannot parse xml file E:\project\.idea\workspace.xml: Error on line 1: 前言中不允许有内容。
因为电脑卡死强制重启电脑后打开idea,进行junit单元测试报错: idea报错.Error:Failed to load project configuration: cannot parse x ...
- Android仿微信进度弹出框的实现方法
MainActivity: package com.ruru.dialogproject; import android.app.Activity; import android.os.Bundle; ...
- H5操作WebSQL数据库
HTML代码: <!DOCTYPE html> <html> <head lang="en"> <meta charset="U ...
- 51Nod 最长的循环节(打表预处理)
正整数k的倒数1/k,写为10进制的小数如果为无限循环小数,则存在一个循环节,求<=n的数中,倒数循环节长度最长的那个数,假如存在多个最优的答案,输出所有答案中最大的那个数. 1/6= ...
- python(2) 图像通道,几何变换,裁剪
一.图像通道 1.彩色图像转灰度图 from PIL import Image import matplotlib.pyplot as plt img=Image.open('d:/ex.jpg') ...
- 洛谷2474 [SCOI2008] 天平 差分约束->枚举
题目描述 你有n个砝码,均为1克,2克或者3克.你并不清楚每个砝码的重量,但你知道其中一些砝码重量的大小关系.你把其中两个砝码A 和B 放在天平的左边,需要另外选出两个砝码放在天平的右边.问:有多少种 ...
- 洛谷P1164 小A点菜 && caioj 1410 动态规划1:点菜(背包方案问题)
方程很简单 f[0] = 1 f[j] += f[j-w[i]] #include<cstdio> #define REP(i, a, b) for(int i = (a); i < ...
- ArcGIS api for javascript——图形-增加图形到地图
描述 本例展示了如何使用Draw工具栏在地图上描绘许多种类的几何体.ArcGIS JavaScript API包含工具栏. 工具栏不是一个在页面上自动地可见的用户界面组件.相反,工具栏是一个助手类,可 ...