USACO Section 1.4 - Mother's Milk(DFS + 记忆化)

题意

有三个桶,刚开始第三个是满的,问第一个是0的时候第三个桶的情况。

思路

无脑倒水,只要有水就倒。记录每一种状态是否达到过。

代码

 
 
  1. /*
  2. ID: mycodeb1
  3. LANG: C++
  4. TASK: milk3
  5. */
  6. #include <bits/stdc++.h>
  7. #define LL long long
  8. #define lowbit(x) ((x) & (-x))
  9. #define MP(a, b) make_pair(a, b)
  10. const int MAXN = 25 + 5;
  11. const int INF = 0x3f3f3f3f;
  12. using namespace std;
  13. int aFull, bFull, cFull, dp[MAXN][MAXN][MAXN];
  14. set<int> st;
  15. void DFS(int a, int b, int c)
  16. {
  17. if (dp[a][b][c]) return;
  18. dp[a][b][c] = 1;
  19. if (a == 0) st.insert(c);
  20. if (a != 0)
  21. {
  22. if (b < bFull)
  23. DFS(a - min(a, bFull - b), b + min(a, bFull - b), c);
  24. if (c < bFull)
  25. DFS(a - min(a, cFull - c), b, c + min(a, cFull - c));
  26. }
  27. if (b != 0)
  28. {
  29. if (a < aFull)
  30. DFS(a + min(b, aFull - a), b - min(b, aFull - a), c);
  31. if (c < cFull)
  32. DFS(a, b - min(b, cFull - c), c + min(b, cFull - c));
  33. }
  34. if (c != 0)
  35. {
  36. if (a < aFull)
  37. DFS(a + min(c, aFull - a), b, c - min(c, aFull - a));
  38. if (b < bFull)
  39. DFS(a, b + min(c, bFull - b), c - min(c, bFull - b));
  40. }
  41. }
  42. int main()
  43. {
  44. //freopen("input.txt", "r", stdin);
  45. freopen("milk3.in", "r", stdin);
  46. freopen("milk3.out", "w", stdout);
  47. int a, b, c, i, j;
  48. scanf("%d%d%d", &aFull, &bFull, &cFull);
  49. DFS(0, 0, cFull);
  50. for (set<int>::iterator it = st.begin(); it != st.end(); it++)
  51. {
  52. if (it == st.begin()) printf("%d", *it);
  53. else printf(" %d", *it);
  54. }
  55. puts("");
  56. return 0;
  57. }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值