您的位置:首页 > 其它

HDU 1078 记忆化搜索

2015-08-12 00:05 531 查看
FatMouse and Cheese

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

Total Submission(s): 6454 Accepted Submission(s): 2627

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

自顶向下分析:

从(0,0)出发,开始遍历每一条可能的路径,当遍历到周围能达到的数都比自己小时返回。记录此时的总值,与变量Max比较,记下较大的数,最终的Max就是要求的值。

自底向上分析:

当遍历到最后时,返回这时的元素值到上一层。对中间的过程用动态规划考虑,dp[i][j]代表的是(i,j)能延伸的最大距离和,转移方程dp[i][j]=max{dp[x][y]}+G[i][j] (x,y为从i,j能延伸到的范围)接着将找到结果点存到dp[]里,下次只要dp[]不为0,就直接调用dp[]而不用递归。

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
//#define LOCAL
using namespace std;
int dp[105][105];
int G[105][105];
const int V[4][2] = {0,-1,0,1,-1,0,1,0};
int n,k;
int dfs(int i,int j);
bool IsTrue(int a,int b);
int main()
{
#ifdef LOCAL
freopen("data.in","r",stdin);
freopen("data.out","w",stdout);
#endif // LOCAL

while(scanf("%d%d",&n,&k)&&n!=-1&&k!=-1){
int i,j;
for(i = 0;i<n;i++)
for(j = 0;j<n;j++)
scanf("%d",&G[i][j]);
memset(dp,0,sizeof(dp));
printf("%d\n",dfs(0,0));
}
return 0;
}

bool IsTrue(int a,int b){
if(0<=a&&a<n&&0<=b&&b<n)
return true;
return false;
};

int dfs(int i,int j){
int t,T;
int sum = 0;
int ans;
if(dp[i][j] != 0)
return dp[i][j];
else{
for(T = 0;T<4;T++)
for(t = 1;t<=k;t++)
if(IsTrue(i+t*V[T][0],j+t*V[T][1])&&G[i][j]<G[i+t*V[T][0]][j+t*V[T][1]]){
ans = dfs(i+t*V[T][0],j+t*V[T][1]);
if(ans>sum)
sum = ans;
}
return dp[i][j] =  sum + G[i][j];
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  搜索 动态规划 杭电