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
一般的代码
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);
}
这个时间会超时 会很浪费时间 因为会出现同一个数多次 如果我们能够标记这个数 当下次遇到这个数的时候就直接输出就可以
#include <stdio.h>
#include <algorithm>
#include <cstring>
using namespace std;
int mu[100][100][100];
int w(int a,int b,int c)
{
if(a<=0||b<=0||c<=0) return 1;
if(mu[a][b][c]) return mu[a][b][c];
if(a>20||b>20||c>20) return w(20,20,20);
if(a < b && b < c) return mu[a][b][c] = w(a,b,c-1) + w(a,b-1,c-1) - w(a,b-1,c);
else return mu[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 n,m,k;
while(scanf("%d%d%d",&n,&m,&k)!=EOF&&n!=-1||m!=-1||k!=-1)
{
memset(mu,0,sizeof(mu));
printf("w(%d, %d, %d) = %d\n",n,m,k,w(n,m,k));
}
}
滑雪
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
Sample Output
4
Sample Input
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
7
hint
样例1:7->6->4->3 长度为4
思路就是暴力解法 便利所有数 从所有数中寻找最优解 然后统计每个数的最大段。但是要用到记忆化搜索 依旧是标记这个点的最优点 下次遇到这个点就直接输出。
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
int str[100][100];//存图
int vis[100][100];//负责记忆化数组 记录这个点的最优解
int ds[4][2]={1,0,-1,0,0,1,0,-1};
int n,m,sum=0;
int dfs(int x,int y)
{
if(vis[x][y]) return vis[x][y];
int len=1;
int maxlen=1;
for(int i=0;i<4;i++)
{
int dx=x+ds[i][0];
int dy=y+ds[i][1];
if(dx>=0&&dy>=0&&dx<n&&dy<m&&str[x][y]>str[dx][dy])
{
len=dfs(dx,dy)+1;//长度加1
maxlen=max(len,maxlen);//寻找所有点的最大len
}
}
return maxlen;
}
int main()
{
memset(str,0,sizeof(str));
cin>>n>>m;
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
cin>>str[i][j];
}
}
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
vis[i][j]=dfs(i,j);//对每个点都便利寻找最优解
sum=max(sum,vis[i][j]);//寻找所有点中最优解
}
}
cout<<sum<<endl;
return 0;
}
浅谈背包问题用到记忆化搜索
普通的搜索方法
int w[1000];//保存物品的重量
int v[1000];//保存物品的价值
//从第i个物品开始挑选总重量小于j的部分
int dfs(int i,int j)
{
int res;
if(i==n) res=0;//已经没有物品
else if(j<w[i])
{
res=dfs(i+1,j);//无法挑选
}
else res=max(dfs(i+1,j),dfs(i+1,j-w[i])+v[i]);
return res;
}
用记忆化搜索的代码
int w[1000];//保存物品的重量
int v[1000];//保存物品的价值
int dp[1000][1000]; //记忆化数组,要初始化为-1
int n;//n个物品
//从第i个物品开始挑选总重量小于j的部分
int dfs(int i,int j)
{
if(dp[i][j]>=0) return dp[i][j];//已经计算过的话直接返回结果
int res;
if(i==n) res=0;//已经没有物品
else if(j<w[i])
{
res=dfs(i+1,j);//无法挑选
}
else res=max(dfs(i+1,j),dfs(i+1,j-w[i])+v[i]);//挑选和不挑选两种情况
return dp[i][j]=res;//保存结果并返回
}