hdu 1495

 1 #include<stdio.h>
 2 #include<iostream>
 3 #include<string.h>
 4 #include<queue>
 5 using namespace std;
 6 
 7 struct node
 8 {
 9   int a[3];
10   int len;
11 };
12 
13 queue<node> q;
14 int map[101][101][101];
15 int dx[3];
16 double flag;
17 
18  
19 
20 void bfs(int x,int y,int z)
21 {
22   while(!q.empty())
23   {
24   q.pop();
25   }
26   node p;
27   p.a[0]=x;
28   p.a[1]=y;
29   p.a[2]=z;
30   p.len=0;
31   q.push(p);
32   while(!q.empty())
33   {
34   node p=q.front();
35   q.pop();
36   for(int i=0;i<3;i++)
37   for(int j=0;j<3;j++)
38   {
39     if(i == j) continue;//不能倒给自己 
40     node p1;
41     if(p.a[i] <= dx[j]-p.a[j])//能全部倒进去 
42     {
43     p1.a[j]=p.a[i]+p.a[j];
44     p1.a[i]=0;
45     }
46     else//不能全部倒进去,有剩余 
47     {
48       p1.a[i]=p.a[i]-(dx[j]-p.a[j]);
49       p1.a[j]=dx[j]; 
50     }
51     for(int k=0;k<3;k++)//将剩余的倒回来 
52     {
53       if(k == i||k == j)
54       continue;
55       p1.a[k]=p.a[k];
56     }
57     p1.len=p.len;//记录倒过的次数 
58     if(map[p1.a[0]][p1.a[1]][p1.a[2]] == 1)//判断是否倒过 
59       continue;
60     map[p1.a[0]][p1.a[1]][p1.a[2]]=1;//倒过后做好标记 
61     p1.len++;//次数加一 
62     q.push(p1); 
63     if((flag==p1.a[0]&&p1.a[0]==p1.a[1])||(flag==p1.a[0]&&p1.a[0]==p1.a[2])||(flag==p1.a[1]&&p1.a[1]==p1.a[2]))//判断可乐是否被平分 
64     {
65       cout<<p1.len<<endl;
66       return;
67     }
68 
69    }
70   }
71   cout<<"NO"<<endl;
72 }
73 
74 int main()
75 {
76   int s,m,n;
77   while(scanf("%d %d %d",&s,&m,&n),s,m,n)
78   {
79     memset(map,0,sizeof(map));
80     dx[0]=s;
81     dx[1]=n;
82     dx[2]=m;
83     flag=s/2.0;
84     bfs(s,0,0);
85   }
86 return 0;
87 }

 

posted @ 2015-07-31 10:26  澧浦  阅读(208)  评论(0)    收藏  举报