P1434 [SHOI2002] 滑雪
本题思路:我们可以枚举每个点,然后判断这个点有没有走过,走过就直接return dp[这个点],没走过就算出最大可以走的步数。
为啥呢,因为1≤R,C≤100。
哈哈哈…
P2690 [USACO04NOV] Apple Catching G
本题是一道3维的记搜,如果搜过,直接retrun,如果没搜过,就算出答案,我们还要把它分为2类,1.小于w我们用tmp1存储,2.大于w我们用tmp2存储。
最后取最大值,就可以了。
注意:初始值要设-1 哟。
C++#include <bits/stdc++.h>
using namespace std ;
int t,w,a[1009],vis[1009][19][109] ;
int dfs(int cur,int pos,int cnt)
{
if(cur > t)
{
return 0;
}
if(vis[cur][pos][cnt] != -1)
{
return vis[cur][pos][cnt];
}
int tmp1=0,tmp2=0;
if(cnt < w)
{
tmp1 = dfs(cur + 1 , 3 - pos , cnt + 1) + (3 - pos == a[cur]);
}
tmp2 = dfs(cur + 1 , pos , cnt) + (pos == a[cur]);
return vis[cur][pos][cnt] = max(tmp1 , tmp2) ;
}
int main()
{
cin >> t >> w;
for(int i = 1 ; i <= t ; i ++)
{
cin >> a[i];
}
memset(vis ,-1,sizeof(vis)) ;
cout << dfs(1 , 1 , 0) ;
return 0 ;
}