hdu 1495 可乐

大家一定觉的运动以后喝可乐是一件很惬意的事情,但是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,写了一个三维数组表示100以内三个杯子里的状态,然后注意要判断六种情况,也就是每个杯子分别向
其他杯子里倒水,同时要判断倒没倒满,这个也是一个坑,最后bfs就出来了,多多练习吧,有点怕这题t,但是似乎数据范围不错,队列很快都出来了。
 1 #include <iostream>
 2 #include <queue>
 3 #include <vector>
 4 #include <cstring>
 5 using namespace std;
 6 struct cup{
 7     int cola[3],s;
 8 };
 9 int b[100][100][100];
10 int d[3];
11 int main()
12 {
13     int n,m,s;
14     while(cin>>d[0]>>d[1]>>d[2],d[0]+d[1]+d[2])
15     {
16         memset(b,0,sizeof(b));
17         b[d[0]][d[1]][d[2]]=1;
18         queue<cup> q;
19         struct cup c;
20         c.cola[0]=d[0];
21         c.cola[1]=0;
22         c.cola[2]=0;
23         c.s=0;
24         int flag=0;
25         int half;
26         q.push(c);
27         b[d[0]][d[1]][d[2]]=1;
28         if((d[0])%2==1)
29         {
30             cout<<"NO"<<endl;
31             continue;
32         }
33         else
34         {
35             half=(d[0])/2;
36         }
37         while(!q.empty())
38         {
39             struct cup tmp=q.front();
40             if((tmp.cola[1]==half&&tmp.cola[0]==half)||(tmp.cola[0]==half&&half==tmp.cola[2])||(tmp.cola[2]==half&&half==tmp.cola[1]))
41             {
42                 cout<<tmp.s<<endl;
43                 flag=1;
44                 break;
45             }
46             q.pop();
47             for(int i=0;i<3;i++)
48             {
49                 
50                 if(tmp.cola[i]>0)
51                 {  
52                     for(int j=0;j<3;j++)
53                     {    
54                         struct cup tt=tmp;
55                         if(i==j)
56                         {
57                             continue;
58                         }
59                         if(tt.cola[i]>d[j]-tt.cola[j])
60                         {
61                              tt.cola[i]-=d[j]-tt.cola[j];
62                              tt.cola[j]=d[j];
63                         }
64                         else
65                         {
66                             tt.cola[j]+=tt.cola[i];
67                             tt.cola[i]=0;
68                         }
69                         if(b[tt.cola[0]][tt.cola[1]][tt.cola[2]]!=1)
70                         {
71                             tt.s++;
72                             b[tt.cola[0]][tt.cola[1]][tt.cola[2]]=1;
73                             q.push(tt);
74                         }
75                     }
76                 }
77             }
78         }
79         if(flag==0)
80         cout<<"NO"<<endl;
81     }
82 }

猜你喜欢

转载自www.cnblogs.com/coolwx/p/11124071.html