记忆化搜索之 A - Function Run Fun B - 滑雪

记忆化搜索
A - Function Run Fun
题目:链接:A - Function Run Fun
We all love recursion! Don’t we?
Consider a three-parameter recursive function w(a, b, c):
if a <= 0 or b <= 0 or c <= 0, then w(a, b, c) returns: 1
if a > 20 or b > 20 or c > 20, then w(a, b, c) returns: w(20, 20, 20)
if a < b and b < c, then w(a, b, c) returns: w(a, b, c-1) + w(a, b-1, c-1) - w(a, b-1, c)
otherwise it returns: w(a-1, b, c) + w(a-1, b-1, c) + w(a-1, b, c-1) - w(a-1, b-1, c-1)
This is an easy function to implement. The problem is, if implemented directly, for moderate values of a, b and c (for example, a = 15, b = 15, c = 15), the program takes hours to run because of the massive recursion.
input:
The input for your program will be a series of integer triples, one per line, until the end-of-file flag of -1 -1 -1. Using the above technique, you are to calculate w(a, b, c) efficiently and print the result.
output:
Print the value for w(a,b,c) for each triple.
Sample Input:
1 1 1
2 2 2
10 4 6
50 50 50
-1 7 18
-1 -1 -1
Sample Output:
w(1, 1, 1) = 2
w(2, 2, 2) = 4
w(10, 4, 6) = 523
w(50, 50, 50) = 1048576
w(-1, 7, 18) = 1
code:
记忆化搜索:

#include<iostream>
#include<cstring>
//#include<bits/stdc++.h>
using namespace std;
int dp[100][100][100];
int num;
int w(int a,int b,int c)
{
	if(dp[a][b][c]) return dp[a][b][c];//如果计算过就直接返回//记忆化搜索 
	//if a <= 0 or b <= 0 or c <= 0,returns: 1 
	if( a<=0 || b<=0 || c<=0 ) return dp[a][b][c]=1;
	//if a > 20 or b > 20 or c > 20, then w(a, b, c) returns: w(20, 20, 20) 
	if( a>20 || b>20 || c>20 ) return dp[a][b][c]=w(20,20,20);
	//if a < b and b < c, then w(a, b, c) returns: w(a, b, c-1) + w(a, b-1, c-1) - w(a, b-1, c)
	if( a<b && b<c ) return  dp[a][b][c]=w(a,b,c-1)+w(a,b-1,c-1)-w(a,b-1,c);
	//otherwise it returns: w(a-1, b, c) + w(a-1, b-1, c) + w(a-1, b, c-1) - w(a-1, b-1, c-1)
	else return dp[a][b][c]=w(a-1, b, c) + w(a-1, b-1, c) + w(a-1, b, c-1) - w(a-1, b-1, c-1);
 
}
int main()
{
	int a,b,c;
	memset(dp,0,sizeof(dp));
	while(scanf("%d%d%d",&a,&b,&c)!=EOF)
	{
		//cout<<a<<" "<<b<<" "<<c<<endl;
		if(a==-1 && b==-1 && c==-1) break;
		int d=w(a,b,c);
		if( a<0 || b<0 || c<0 )
			printf("w(%d, %d, %d) = 1\n",a,b,c);
		else
			printf("w(%d, %d, %d) = %d\n",a,b,c,d);
	}
	return 0;
}

一般搜索:

int dfs(int a,int b,int c)
{
if(a <= 0 || b <= 0 || c <= 0) return 1;
if(a > 20 || b > 20 || c > 20) return dfs(20,20,20);
if(a < b && b < c) return dfs(a,b,c-1) + dfs(a,b-1,c-1) - dfs(a,b-1,c);
else return dfs(a-1,b,c) + dfs(a-1,b-1,c) + dfs(a-1,b,c-1) - dfs(a-1,b-1,c-1);
}

记忆化搜索的好处在于每进行一次搜索,都将把搜索的值记录下来,以便下次直接使用,不必再重新求解,大大降低了运算的复杂程度。
下面的一道题也用到了记忆化搜索:
B - 滑雪
题目:链接:B - 滑雪
Glory非常喜欢玩滑滑梯游戏,下面给出了一个n,m的滑道,其中的数字表示滑道的高度。Glory可以从一个点出发向下滑行,每次只能滑行到相邻的位置(上下左右)中高度严格低于当前高度的地方,不能重复划行已经滑行过的地方,但他希望在这个滑道上滑行尽量远的距离,也即是找一条最长的滑道。
input:
第一行输入两个数n,m代表滑梯范围行n和列m(1 <= n,m <= 100)。下面是n行,每行有m个整数,代表高度h,(0<=h<=20000)
output:
输出一个值,代表Glory能够在滑滑梯上面滑行的最长长度是多少
Sample Input:
3 3
9 1 2
5 6 7
8 4 3
4 7
7 6 5 4 3 2 1
1 5 1 1 1 1 1
1 4 3 1 1 1 1
1 5 6 7 8 1 1
Sample Output:
4
7
hint:
样例1:7->6->4->3 长度为4
code:

#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
int d[4][2] = {0,1,0,-1,1,0,-1,0};//方向 
int n,m;//n行m列 
int map[105][105];
int dp[105][105]; 
int dfs(int x,int y)
{
	if(dp[x][y]) return dp[x][y];//如果已计算过,则直接返回//记忆式搜索
	int len;
	int res=1;
	for(int i=0;i<4;i++)//四个方向
	{
		int dx=x+d[i][0];
		int dy=y+d[i][1];//更新移动后的坐标 
		if( dx>0 && dx<=n && dy>0 && dy<=m && map[dx][dy]<map[x][y])//没越界并且下一步要比上一步低
		{
			len=dfs(dx,dy)+1;//记录每次的长度//b 
			res=max(res,len);//更新最长的长度 //b
		 } 
	}
	return dp[x][y]=res;//把每次求得的 dp[i][j]存储起来 
}
int main()
{
	while(cin>>n>>m)
	{
		for(int i=1;i<=n;i++)
			for(int j=1;j<=m;j++)
				cin>>map[i][j];//存图
		int ans=0;
		memset(dp,0,sizeof(dp));//每次测试数据循环都要将dp清零,map重新赋值 
		for(int i=1;i<=n;i++)
			for(int j=1;j<=m;j++)
			{
				dp[i][j]=dfs(i,j);//dfs(i,j)返回的是在起点(i,j)处的最大长度//a
				ans=max(ans,dp[i][j]);//a
				//不断更新当循环到(i,j)时,(i,j)及(i,j)以前的所有点所能达到的最大长度的最大值 
			}
		cout<<ans<<endl; 
	}
	return 0;
}

分析:
由于是求最长的滑行长度,所以要把从每个点出发所能达到的最大滑行长度全都计算出来,然后再取最大值。
上述代码a处:
dfs(i,j)所表示的就是从(i,j)出发所能滑行的最大长度,并且在每次循环过程中将对应坐标对应的最大滑行长度用 ans 更新,ans表示的就是循环到当前坐标所能滑行的最大长度;
上述代码b处:
dfs函数执行的操作是从坐标(i,j)出发一直往地处走所能滑行的最大长度;从坐标(i,j)出发可能有多种滑行方式,对应的滑行长度也有len 1,len 2,len 3…多种。len=dfs(dx,dy)+1表示的是每走一步只要满足条件,能滑行的长度len就加1,并且继续搜索dfs(dx,dy);res=max(res,len)的作用是在所有的len 1,len 2,len 3…中不断更新最大的len,并将其存到dfs(i,j)里。
值得一提的是:
采用记忆化搜索
以上述第一个样例输入:
3 3
9 1 2
5 6 7
8 4 3
为例:
比如搜索从6开始的能滑行的最大长度,其中有一条路径是6->4->3。在这个搜索过程中已经搜索过从4开始能滑行的最大长度dp[3][2],并且进行了记录。
那么在搜索从4(第3行第2列dp[3][2])开始能滑行的最大长度时,dp[3][2]可以直接调用,不用再次计算,大大降低了复杂度。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值