HDOJ 1495 非常可乐

因为没有刻度,所以X->Y只有两种情况:

1:X倒完了

2:Y倒满了

非常可乐

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3112    Accepted Submission(s): 1273


Problem 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
 
Author
seeyou
 
Source
 
Recommend
LL
 
 1 #include <iostream>
 2 #include <cstring>
 3 #include <queue>
 4 
 5 using namespace std;
 6 
 7 struct dot
 8 {
 9     int bm[3];
10     int step;
11 };
12 
13 int vis[110][110][110];
14 
15 int bfs(int a,int b,int c)
16 {
17     int st[3];
18     st[0]=a;st[1]=b;st[2]=c;
19     dot node;
20     if(a&1)  return -1;
21     node.bm[0]=a;node.bm[1]=0;node.bm[2]=0;node.step=0;
22     memset(vis,0,sizeof(vis));
23     vis[a][0][0]=1;
24     queue<dot> q;
25     int half=a>>1;
26  //   cout<<half<<endl;
27     q.push(node);
28     while(!q.empty())
29     {
30        dot cur,temp;
31        cur=q.front();  q.pop();
32 //       for(int i=0;i<3;i++)  cout<<cur.bm[i]<<",";
33 //       cout<<"..."<<endl;
34        if((cur.bm[0]==half&&cur.bm[1]==half)||(cur.bm[2]==half&&cur.bm[1]==half)||(cur.bm[0]==half&&cur.bm[2]==half)) return cur.step;
35        cur.step++;
36        for(int i=0;i<3;i++)
37        {
38            for(int j=0;j<3;j++)
39            {
40                if(i==j)  continue;
41                temp=cur;
42                if(temp.bm[i]<=st[j]-temp.bm[j])///i倒完
43                {
44                    temp.bm[j]+=temp.bm[i];
45                    temp.bm[i]=0;
46                }
47                else///j倒满
48                {
49                    temp.bm[i]-=st[j]-temp.bm[j];
50                    temp.bm[j]=st[j];
51                }
52 
53                if(!vis[temp.bm[0]][temp.bm[1]][temp.bm[2]])
54                {
55                    vis[temp.bm[0]][temp.bm[1]][temp.bm[2]]=1;
56                    q.push(temp);
57                }
58            }
59        }
60     }
61     return -1;
62 }
63 
64 int main()
65 {
66     int a,b,c;
67 while((cin>>a>>b>>c)&&(a||b||c))
68 {
69     int ans=bfs(a,b,c);
70     if(ans==-1)
71         cout<<"NO"<<endl;
72     else
73         cout<<ans<<endl;
74 }
75 
76     return 0;
77 }
原文地址:https://www.cnblogs.com/CKboss/p/3100390.html