UVa 10118 免费糖果(记忆化搜索+哈希)
2017-02-08 14:10
134 查看
https://vjudge.net/problem/UVA-10118
题意:
桌上有4堆糖果,每堆有N颗。佳佳有一个最多可以装5颗糖的小篮子。他每次选择一堆糖果,把最顶上的一颗拿到篮子里。如果篮子里有两颗颜色相同的糖果,佳佳就把它们从篮子里拿出来放到自己的口袋里。如果篮子满了而里面又没有相同颜色的糖果,游戏结束。
思路:
怎么判断糖果是否颜色相同是本题的一个重点,看了别人的思路后明白了用哈希是很好的办法。
因为一共只有20颗种类的糖果,所以只需要一个20大小的数组来判断糖果是否出现过。
top[5]数组用来标记每堆糖果所拿的个数。
题意:
桌上有4堆糖果,每堆有N颗。佳佳有一个最多可以装5颗糖的小篮子。他每次选择一堆糖果,把最顶上的一颗拿到篮子里。如果篮子里有两颗颜色相同的糖果,佳佳就把它们从篮子里拿出来放到自己的口袋里。如果篮子满了而里面又没有相同颜色的糖果,游戏结束。
思路:
怎么判断糖果是否颜色相同是本题的一个重点,看了别人的思路后明白了用哈希是很好的办法。
因为一共只有20颗种类的糖果,所以只需要一个20大小的数组来判断糖果是否出现过。
top[5]数组用来标记每堆糖果所拿的个数。
#include<iostream> #include<string> #include<cstring> #include<sstream> #include<algorithm> using namespace std; const int maxn = 45; int n; int c[4][maxn]; int d[maxn][maxn][maxn][maxn]; int top[5]; bool Hash[25]; int dp(int cur) { int& ans = d[top[0]][top[1]][top[2]][top[3]]; if (ans) return ans; if (cur == 5) return 0; //篮子满了 for (int i = 0; i < 4; i++) { if (top[i] == n) continue; //已经取完 int x = c[i][top[i]++]; if (Hash[x] == true) { Hash[x] = false; ans = max(ans, dp(cur - 1) + 1); Hash[x] = true; } else { Hash[x] = true; ans = max(ans, dp(cur + 1)); Hash[x] = false; } top[i]--; } return ans; } int main() { //freopen("D:\\txt.txt", "r", stdin); while (cin >> n && n) { memset(d, 0, sizeof(d)); memset(top, 0, sizeof(top)); memset(Hash, false, sizeof(Hash)); for (int i = 0; i < n;i++) for (int j = 0; j < 4; j++) cin >> c[j][i]; cout << dp(0) << endl; } return 0; }
相关文章推荐
- 算法竞赛入门经典 第二版 习题9-2 免费糖果 Free Candies uva10118
- uva 10118 免费糖果 深搜+记忆化搜索
- Uva 10118 免费糖果
- 习题9-2 免费糖果 UVa10118
- UVa 10118 Free Candies (记忆化搜索+哈希)
- UVa 免费糖果(记忆化搜索)
- UVA 10118 - Free Candies
- 记忆化搜索 逆向dp uva10118
- UVA 10118 Free Candies
- uva 10118 Free Candies+记忆化搜索
- UVa 10118 Free Candies (记忆化搜索+状态压缩)
- UVA - 10118 Free Candies
- uva 10118 记忆化搜索
- UVA 10118 Free Candies
- UVA 10118 - Free Candies(记忆化搜索)
- Free Candies UVA - 10118(记搜)
- uva 10118 Free Candies dp(分析+状压)
- uva10118
- 免费虚拟币糖果更新(2018/02/02)
- Uva 10118 Free Candies (DP+记忆化搜索)