zoukankan      html  css  js  c++  java
  • hdu 1495 非常可乐

    题目连接

    http://acm.hdu.edu.cn/showproblem.php?pid=1495 

    非常可乐

    Description

    大家一定觉的运动以后喝可乐是一件很惬意的事情,但是seeyou却不这么认为。因为每次当seeyou买了可乐以后,阿牛就要求和seeyou一起分享这一瓶可乐,而且一定要喝的和seeyou一样多。但seeyou的手中只有两个杯子,它们的容量分别是N 毫升和M 毫升 可乐的体积为S (S<101)毫升 (正好装满一瓶) ,它们三个之间可以相互倒可乐 (都是没有刻度的,且 S==N+M,101>S>0,N>0,M>0) 。聪明的ACMER你们说他们能平分吗?如果能请输出倒可乐的最少的次数,如果不能输出"NO"。

    Input

    三个整数 : S 可乐的体积 , N 和 M是两个杯子的容量,以"0 0 0"结束。

    Output

    如果能平分的话请输出最少要倒的次数,否则输出"NO"。

    Sample Input

    7 4 3
    4 1 3
    0 0 0

    Sample Output

    NO
    3

    倒水问题直接bfs爆搜,注意一下边界。。

      1 #include<algorithm>
      2 #include<iostream>
      3 #include<cstdlib>
      4 #include<cstring>
      5 #include<cstdio>
      6 #include<vector>
      7 #include<queue>
      8 #include<map>
      9 using std::cin;
     10 using std::cout;
     11 using std::endl;
     12 using std::find;
     13 using std::sort;
     14 using std::map;
     15 using std::pair;
     16 using std::queue;
     17 using std::vector;
     18 using std::multimap;
     19 #define pb(e) push_back(e)
     20 #define sz(c) (int)(c).size()
     21 #define mp(a, b) make_pair(a, b)
     22 #define all(c) (c).begin(), (c).end()
     23 #define iter(c) decltype((c).begin())
     24 #define cls(arr,val) memset(arr,val,sizeof(arr))
     25 #define cpresent(c, e) (find(all(c), (e)) != (c).end())
     26 #define rep(i, n) for (int i = 0; i < (int)(n); i++)
     27 #define tr(c, i) for (iter(c) i = (c).begin(); i != (c).end(); ++i)
     28 const int Max_N = 110;
     29 typedef unsigned long long ull;
     30 bool vis[Max_N][Max_N][Max_N];
     31 int N, M, S;
     32 struct Node {
     33     int x, y, z, s;
     34     Node(int i = 0, int j = 0, int k = 0, int l = 0) :x(i), y(j), z(k), s(l) {}
     35 };
     36 inline bool judge(const Node &q) {
     37     int t = S >> 1;
     38     if (q.x == t && q.y == t) return true;
     39     if (q.x == t && q.z == t) return true;
     40     if (q.y == t && q.x == t) return true;
     41     if (q.y == t && q.z == t) return true;
     42     if (q.z == t && q.y == t) return true;
     43     if (q.z == t && q.x == t) return true;
     44     return false;
     45 }
     46 int bfs() {
     47     if (S & 1) return 0;
     48     int v;
     49     cls(vis, false);
     50     queue<Node> q;
     51     q.push(Node(S, 0, 0, 0));
     52     vis[S][0][0] = true;
     53     while (!q.empty()) {
     54         Node t = q.front(); q.pop();
     55         if (judge(t)) return t.s;
     56         if (t.x > 0 && t.y < N) {
     57             v = N - t.y;
     58             if (v < t.x && !vis[t.x - v][N][t.z]) {
     59                 q.push(Node(t.x - v, N, t.z, t.s + 1));
     60                 vis[t.x - v][N][t.z] = true;
     61             } else {
     62                 if (t.x + t.y <= N && !vis[0][t.x + t.y][t.z]) {
     63                     q.push(Node(0, t.x + t.y, t.z, t.s + 1));
     64                     vis[0][t.x + t.y][t.z] = true;
     65                 }
     66             }
     67         }
     68         if (t.x > 0 && t.z < M) {
     69             v = M - t.z;
     70             if (v < t.x && !vis[t.x - v][t.y][M]) {
     71                 q.push(Node(t.x - v, t.y, M, t.s + 1));
     72                 vis[t.x - v][t.y][M] = true;
     73             } else {
     74                 if (t.z + t.x <= M && !vis[0][t.y][t.z + t.x]) {
     75                     q.push(Node(0, t.y, t.x + t.z, t.s + 1));
     76                     vis[0][t.y][t.x + t.z] = true;
     77                 }
     78             }
     79         }
     80         if (t.y > 0 && t.x < S) {
     81             v = S - t.x;
     82             if (v < t.y && !vis[S][t.y - v][t.z]) {
     83                 q.push(Node(S, t.y - v, t.z, t.s + 1));
     84                 vis[S][t.y - v][t.z] = true;
     85             } else {
     86                 if (t.x + t.y <= S && !vis[t.x + t.y][0][t.z]) {
     87                     q.push(Node(t.x + t.y, 0, t.z, t.s + 1));
     88                     vis[t.x + t.y][0][t.z] = true;
     89                 }
     90             }
     91         }
     92         if (t.y > 0 && t.z < M) {
     93             v = M - t.z;
     94             if (v < t.y && !vis[t.x][t.y - v][M]) {
     95                 q.push(Node(t.x, t.y - v, M, t.s + 1));
     96                 vis[t.x][t.y - v][M] = true;
     97             } else {
     98                 if (t.z + t.y <= M && !vis[t.x][0][t.z + t.y]) {
     99                     q.push(Node(t.x, 0, t.z + t.y, t.s + 1));
    100                     vis[t.x][0][t.z + t.y] = true;
    101                 }
    102             }
    103         }
    104         if (t.z > 0 && t.y < N) {
    105             v = N - t.y;
    106             if (v < t.z && !vis[t.x][N][t.z - v]) {
    107                 q.push(Node(t.x, N, t.z - v, t.s + 1));
    108                 vis[t.x][N][t.z - v] = true;
    109             } else {
    110                 if (t.y + t.z <= N && !vis[t.x][t.y + t.z][0]) {
    111                     q.push(Node(t.x, t.z + t.y, 0, t.s + 1));
    112                     vis[t.x][t.z + t.y][0] = true;
    113                 }
    114             }
    115         }
    116         if (t.z > 0 && t.x < S) {
    117             v = S - t.x;
    118             if (v < t.z && !vis[S][t.y][t.z - v]) {
    119                 q.push(Node(S, t.y, t.z - v, t.s + 1));
    120                 vis[S][t.y][t.z - v] = true;
    121             } else {
    122                 if (t.x + t.z <= S && !vis[t.x + t.z][t.y][0]) {
    123                     q.push(Node(t.x + t.z, t.y, 0, t.s + 1));
    124                     vis[t.x + t.z][t.y][0] = true;
    125                 }
    126             }
    127         }
    128     }
    129     return 0;
    130 }
    131 int main() {
    132 #ifdef LOCAL
    133     freopen("in.txt", "r", stdin);
    134     freopen("out.txt", "w+", stdout);
    135 #endif
    136     while (~scanf("%d %d %d", &S, &N, &M) && S + M + N) {
    137         int res = bfs();
    138         if (!res) puts("NO");
    139         else printf("%d
    ", res);
    140     }
    141     return 0;
    142 }
    View Code
    By: GadyPu 博客地址:http://www.cnblogs.com/GadyPu/ 转载请说明
  • 相关阅读:
    现代软件工程 第一章 概论 第3题——韩婧
    现代软件工程 第一章 概论 第2题——韩婧
    小组成员邓琨、白文俊、张星星、韩婧
    UVa 10892 LCM的个数 (GCD和LCM 质因数分解)
    UVa 10780 幂和阶乘 求n!中某个因子的个数
    UVa 11859 除法游戏(Nim游戏,质因子)
    Codeforces 703C Chris and Road 二分、思考
    Codeforces 703D Mishka and Interesting sum 树状数组
    hdu 5795 A Simple Nim SG函数(多校)
    hdu 5793 A Boring Question 推公式(多校)
  • 原文地址:https://www.cnblogs.com/GadyPu/p/4614395.html
Copyright © 2011-2022 走看看