HDU1495 非常可乐

题目:

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

输入:

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

输出:

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

样例:

分析:挺简单一BFS但是那个循环里的破玩意我打错了好几个的地方,被自己蠢哭了◢▆▅▄▃崩╰(〒皿〒)╯潰▃▄▅▇◣

 

  1 #include<iostream>
  2 #include<sstream>
  3 #include<cstdio>
  4 #include<cstdlib>
  5 #include<string>
  6 #include<cstring>
  7 #include<algorithm>
  8 #include<functional>
  9 #include<iomanip>
 10 #include<numeric>
 11 #include<cmath>
 12 #include<queue>
 13 #include<vector>
 14 #include<set>
 15 #include<cctype>
 16 #define PI acos(-1.0)
 17 const int INF = 0x3f3f3f3f;
 18 const int NINF = -INF - 1;
 19 typedef long long ll;
 20 using namespace std;
 21 struct node
 22 {
 23     int x, y, z;
 24 }st;
 25 int a, b, c;
 26 int d[105][105][105];
 27 int used[105][105][105];
 28 int bfs()
 29 {
 30     queue<node> q;
 31     memset(used, 0, sizeof(used));
 32     for (int i = 0; i <= a; ++i)
 33     {
 34         for (int j = 0; j <= b; ++j)
 35         {
 36             for (int k = 0; k <= c; ++k)
 37                 d[i][j][k] = INF;
 38         }
 39     }
 40     st.x = a, st.y = 0, st.z = 0;
 41     q.push(st);
 42     used[st.x][st.y][st.z] = 1;
 43     d[st.x][st.y][st.z] = 0;
 44     while (q.size())
 45     {
 46         node temp;
 47         temp = q.front();
 48         q.pop();
 49         if ((temp.x == temp.y && temp.z == 0) || (temp.x == temp.z && temp.y == 0) || (temp.y == temp.z && temp.x == 0))
 50             return d[temp.x][temp.y][temp.z];
 51         for (int i = 0; i < 6; ++i)
 52         {
 53             node n = temp;
 54             if (i == 0 && n.x != 0)
 55             {
 56                 if (n.x + n.y >= b)
 57                 {
 58                     n.x -= b - n.y;
 59                     n.y = b;
 60                 }
 61                 else
 62                 {
 63                     n.y += n.x;
 64                     n.x = 0;
 65                 }
 66             }
 67             if (i == 1 && n.x != 0)
 68             {
 69                 if (n.x + n.z >= c)
 70                 {
 71                     n.x -= c - n.z;
 72                     n.z = c;
 73                 }
 74                 else
 75                 {
 76                     n.z += n.x;
 77                     n.x = 0;
 78                 }
 79             }
 80             if (i == 2 && n.y != 0)
 81             {
 82                 if (n.x + n.y >= a)
 83                 {
 84                     n.y -= a - n.x;
 85                     n.x = a;
 86                 }
 87                 else
 88                 {
 89                     n.x += n.y;
 90                     n.y = 0;
 91                 }
 92             }
 93             if (i == 3 && n.y != 0)
 94             {
 95                 if (n.z + n.y >= c)
 96                 {
 97                     n.y -= c - n.z;
 98                     n.z = c;
 99                 }
100                 else
101                 {
102                     n.z += n.y;
103                     n.y = 0;
104                 }
105             }
106             if (i == 4 && n.z != 0)
107             {
108                 if (n.x + n.z >= a)
109                 {
110                     n.z -= a - n.x;
111                     n.x = a;
112                 }
113                 else
114                 {
115                     n.x += n.z;
116                     n.z = 0;
117                 }
118             }
119             if (i == 5 && n.z != 0)
120             {
121                 if (n.y + n.z >= b)
122                 {
123                     n.z -= b - n.y;
124                     n.y = b;
125                 }
126                 else
127                 {
128                     n.y += n.z;
129                     n.z = 0;
130                 }
131             }
132             if (!used[n.x][n.y][n.z])
133             {
134                 used[n.x][n.y][n.z] = 1;
135                 q.push(n);
136                 d[n.x][n.y][n.z] = d[temp.x][temp.y][temp.z] + 1;
137             }
138         }
139     }
140     return -1;
141 }
142 int main()
143 {
144     while (cin >> a >> b >> c)
145     {
146         if (a == 0 && b == 0 && c == 0) break;
147         if (a % 2)
148         {
149             cout << "NO" << endl;
150             continue;
151         }
152         int ans = bfs();
153         if (ans != -1)
154             cout << ans << endl;
155         else cout << "NO" << endl;
156     }
157     return  0;
158 }

 

posted @ 2019-06-09 22:29  Veasky  阅读(273)  评论(0编辑  收藏  举报