UVa 免费糖果(记忆化搜索)
2017-06-06 16:10
295 查看
在状态复杂,信息多且数据小的情况下可以选择考虑记忆化搜索.这题我就做的很zz,一开始已知想打一个dp,
不过太麻烦了.看了解题报告之后才发现记忆化搜索这么简单
不过太麻烦了.看了解题报告之后才发现记忆化搜索这么简单
#include<iostream> #include<cstring> #define fo(i,a,b) for(int i=a;i<=b;i++) #define fod(i,a,b) for(int i=a;i>=b;i--) using namespace std; const int N=40+5,M=20+5; int dp ,top[5],pile[5] , n; bool Hash ; void Init() { fo(i,0,n-1) fo(j,0,3) scanf("%d",&pile[j][i]); memset(dp,-1,sizeof(dp)); memset(top,0,sizeof(top)); memset(Hash,0,sizeof(Hash)); } int dfs(int count, bool Hash[]) { if (dp[top[0]][top[1]][top[2]][top[3]] != -1) return dp[top[0]][top[1]][top[2]][top[3]]; if (count == 5) return dp[top[0]][top[1]][top[2]][top[3]] = 0; int ans = 0; for (int i = 0; i < 4; i++) { if (top[i] == n) continue; int color = pile[i][top[i]]; top[i] += 1; if (Hash[color]) { Hash[color] = false; ans = max(ans, dfs(count-1, Hash) + 1); Hash[color] = true; } else { Hash[color] = true; ans = max(ans, dfs(count+1, Hash)); Hash[color] = false; } top[i] -= 1; } return dp[top[0]][top[1]][top[2]][top[3]] = ans; } int main() { while(scanf("%d",&n)&&n) { Init(); printf("%d\n",dfs(0,Hash)); } return 0; }
相关文章推荐
- uva 10118 免费糖果 深搜+记忆化搜索
- 习题9-2 免费糖果 UVa10118
- 习题9-2(免费糖果)【深搜dfs】+【记忆化搜索】
- UVa 10118 免费糖果(记忆化搜索+哈希)
- 算法竞赛入门经典 第二版 习题9-2 免费糖果 Free Candies uva10118
- Uva 10118 免费糖果
- 【BZOJ1589】[Usaco2008 Dec]Trick or Treat on the Farm 采集糖果【SCC】【基环外向树】【DP】【记忆化搜索】
- uva 10626 Buying Coke (DP + 记忆化搜索)
- UVA 10859 Placing Lampposts(记忆化搜索)
- UVA 11884 A Shooting Game(记忆化搜索)
- uva 10400 Game Show Math(记忆化搜索)
- uva 825 Walking on the Safe Side(动态规划:记忆化搜索)
- uva1637(记忆化搜索,离散概率)
- UVa 10118 Free Candies(记忆化搜索经典)
- JD 1550:分糖果(线性dp + 记忆化搜索)
- uva 10688 - The Poor Giant(记忆化搜索)
- Uva 437 DAG上的DP 记忆化搜索实现
- 【HDU1176】免费馅饼(记忆化搜索)
- UVA - 10626 Buying Coke(记忆化搜索)
- 区间dp||记忆化搜索 Game of Sum UVA - 10891 or Light OJ 1031---Easy Game(区间DP)