您的位置:首页 > 其它

hdu 1078 FatMouse and Cheese

2016-07-19 22:03 197 查看
题目链接:hdu 1078 


FatMouse and Cheese

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 8158    Accepted Submission(s): 3411

Problem Description

FatMouse has stored some cheese in a city. The city can be considered as a square grid of dimension n: each grid location is labelled (p,q) where 0 <= p < n and 0 <= q < n. At each grid location Fatmouse has hid between 0 and 100 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. 

 

Input

There are several test cases. Each test case consists of 

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. 

 

Output

For each test case output in a line the single integer giving the number of blocks of cheese collected. 

 

Sample Input

3 1
1 2 5
10 11 6
12 12 7
-1 -1

 

Sample Output

37

 

Source

Zhejiang University Training Contest 2001

题目大意: 一个老鼠去洞外面找吃的,因为外面有猫盯着,最多只能走 K 步(垂直上下左右),吃到东西后,老鼠会长大,只能吃比它当前所吃的要大( 其实就是下一个  mp[i][j]  要比前一个大),求老鼠吃到最多的东西;

解题思路: 这个题是关于动态规划的题目,用记忆化搜素比较好,减少了一些递归运算,其实,但凡懂一点搜索(DFS)的应该很轻易的就把它求出来了,就是上下左右的搜一遍,把数据存在一个数组里面就好了,这是到水题,不多说了,上代码,

#include<iostream>
#include<algorithm>
#include<vector>
#include<map>
#include<string>
#include<cstdio>
#include<cstring>
#include<cctype>
using namespace std;
const int N = 1009;
int mp

, n, k, dp

;
int dfs(int i, int j)
{
/*if(i < 1 || i > n || j < 1 || j > n)//检查下是否越界,下面已经判断过了,就不要多此一举了,
return 0;*/
int ans = 0;//记录最大值
if(!dp[i][j])//记忆化搜索的关键
{
for(int ii = 1; ii <= k; ii++)//搜到k,
{
if(i - ii > 0 && mp[i - ii][j] > mp[i][j])//上走
ans = max(ans, dfs(i - ii, j));
if(j - ii > 0 && mp[i][j - ii] > mp[i][j])//左走
ans = max(ans, dfs(i, j - ii));
if(j + ii <= n && mp[i][j + ii] > mp[i][j])//右走
ans = max(ans, dfs(i, j + ii));
if(i + ii <= n && mp[i + ii][j] > mp[i][j])//下走
ans = max(ans, dfs(i + ii, j));
}
return dp[i][j] = ans + mp[i][j];//赋值并返回
}
return dp[i][j];

}

int main()
{
int i, j;
while(scanf("%d%d", &n, &k), n + k != -2)
{
for(i = 1; i <= n; i++)//输入数据
for(j = 1; j <= n; j++)
scanf("%d", &mp[i][j]);
memset(dp, 0, sizeof(dp));
int ans = dfs(1, 1);
cout << ans << endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: