记忆化搜索(DP)

记忆化搜索(Memory search):在普通搜索的基础上记录一部分已计算的结果从而避免重复计算。

 

例题:

滑雪

 

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 104767   Accepted: 39905

Description

Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激。可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你。Michael想知道载一个区域中最长底滑坡。区域由一个二维数组给出。数组的每个数字代表点的高度。下面是一个例子 

 1  2  3  4 5

16 17 18 19 6

15 24 25 20 7

14 23 22 21 8

13 12 11 10 9


一个人可以从某个点滑向上下左右相邻四个点之一,当且仅当高度减小。在上面的例子中,一条可滑行的滑坡为24-17-16-1。当然25-24-23-...-3-2-1更长。事实上,这是最长的一条。

Input

输入的第一行表示区域的行数R和列数C(1 <= R,C <= 100)。下面是R行,每行有C个整数,代表高度h,0<=h<=10000。

Output

输出最长区域的长度。

Sample Input

5 5
1 2 3 4 5
16 17 18 19 6
15 24 25 20 7
14 23 22 21 8
13 12 11 10 9

Sample Output

25

Source

POJ1088

题意:给一个存高度的map问从任意一点出发(可以向上下左右走)的最长连续递减序列。

 

#include <cstdio>
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
int r,c,map[105][105],ans[105][105],len=1;
int dx[4]={0,1,0,-1},dy[4]={1,0,-1,0};
  
int dfs(int x,int y)  
{
	int sum=1;  //长度至少为1
	if (ans[x][y])
		return ans[x][y]; //如果ans[x][y]已经计算出来,就直接返回不需再次进行计算
	for (int i=0;i<4;i++)  //向上下左右移动
	{
		int nx=x+dx[i],ny=y+dy[i];
			if (nx>=1&&nx<=r&&ny>=1&&ny<=c&&map[nx][ny]<map[x][y])
				sum=max(dfs(nx,ny)+1,sum);  //更新最长长度
	}
	return ans[x][y]=sum; //将计算出的结果存入ans[x][y],方便下次取用
}
  
int main()
{
	scanf("%d%d",&r,&c); 
	memset(map,0,sizeof(map));
	memset(ans,0,sizeof(ans));

	for (int i=1;i<=r;i++)
		for (int j=1;j<=c;j++)
			scanf("%d",&map[i][j]);

	for (int i=1;i<=r;i++)
		for (int j=1;j<=c;j++)
			if (ans[i][j]==0)  //当前的位置还未被搜索
				len=max(len,dfs(i,j));  //更新最大长度

	printf("%d\n",len); 
	return 0;
}

 

 

例题2:

FatMouse and Cheese

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 14661    Accepted Submission(s): 6203


 

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

HDU1078

 

题意:有一种游戏是的玩法是这样的:
有一个n*n的格子,每个格子有一个数字。
遵循以下规则:
1. 玩家每次可以由所在格子向上下左右四个方向进行直线移动,每次移动的距离不得超过m
2. 玩家一开始在第一行第一列,并且已经获得该格子的分值
3. 玩家获得每一次移动到的格子的分值
4. 玩家下一次移动到达的格子的分值要比当前玩家所在的格子的分值要大。
5. 游戏所有数字加起来也不大,保证所有数字的和不会超过int型整数的范围
6. 玩家仅能在n*n的格子内移动,超出格子边界属于非法操作
7. 当玩家不能再次移动时,游戏结束
现在问你,玩家所能获得的最大得分是多少?

显然是一个记忆化搜索

 

代码:

#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;

int n,m,Map[105][105],dp[105][105];
int dx[4]={0,-1,0,1};
int dy[4]={-1,0,1,0};

int dfs(int x,int y)
{
	if (dp[x][y])
		return dp[x][y];
	int ans=0;
	for (int k=1;k<=m;k++)
		for (int i=0;i<4;i++)
		{
			int nx=x+dx[i]*k,ny=y+dy[i]*k;
			if (1<=nx && nx<=n && 1<=ny && ny<=n && Map[nx][ny]>Map[x][y])
				ans=max(dfs(nx,ny),ans);
		}
	return dp[x][y]=ans+Map[x][y];
}

int main()
{
	while(scanf("%d%d",&n,&m))
	{
		if (n==-1 && m==-1)
			break;
		memset(dp,0,sizeof(dp));
		for (int i=1;i<=n;i++)
			for (int j=1;j<=n;j++)
				scanf("%d",&Map[i][j]);
		cout << dfs(1,1) << endl;
	}
	return 0;
}

 

posted on 2018-05-12 17:43  Radium_1209  阅读(212)  评论(0)    收藏  举报

导航