非常可乐(bfs)

非常可乐

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

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

题解:没有初始化,错了半天。。。。。。我真是无语了;直接队列定义在bfs里面就行了

代码1:

  1 #include<iostream>
  2 #include<cstdio>
  3 #include<cstring>
  4 #include<cmath>
  5 #include<algorithm>
  6 #include<queue>
  7 using namespace std;
  8 const int INF=0x3f3f3f3f;
  9 int S,M,N;
 10 int vis[110][110];
 11 struct Node{
 12     int s,m,n,step;
 13 };
 14 bool check(Node a){
 15     int x,y,z;
 16     x=a.s;y=a.m;z=a.n;
 17     int flot=0;
 18     if(!x)flot++;if(!y)flot++;if(!z)flot++;
 19     if(flot!=1)return false;
 20     flot=0;
 21     if(x)if(x*2==S)flot++;
 22     if(y)if(y*2==S)flot++;
 23     if(z)if(z*2==S)flot++;
 24     if(flot==2)return true;
 25     else return false;
 26 }
 27 void bfs(){
 28     Node a,b;
 29     memset(vis,0,sizeof(vis));
 30     queue<Node>dl;
 31     a.s=S;
 32     a.m=0;
 33     a.n=0;
 34     a.step=0;
 35     dl.push(a);
 36     vis[a.m][a.n]=1;
 37     while(!dl.empty()){
 38         a=dl.front();
 39         dl.pop();
 40         if(a.m<M){//s-m
 41             if(a.s>=M-a.m){
 42             b.s=a.s-(M-a.m);
 43             b.m=M;
 44             b.n=a.n;
 45             b.step=a.step+1;
 46             if(check(b)){
 47                 printf("%d
",b.step);
 48                 return;
 49             }
 50             if(!vis[b.m][b.n]){
 51                 vis[b.m][b.n]=1;
 52                 dl.push(b);
 53                 }
 54             }
 55             else{
 56                 b.m=a.m+a.s;
 57                 b.s=0;
 58                 b.n=a.n;
 59                 b.step=a.step+1;
 60                 if(check(b)){
 61                 printf("%d
",b.step);
 62                 return;
 63             }
 64             if(!vis[b.m][b.n]){
 65                 vis[b.m][b.n]=1;
 66                 dl.push(b);
 67                 }
 68             }
 69         }
 70         if(a.n<N){//s-n
 71             if(a.s>=N-a.n){
 72             b.s=a.s-(N-a.n);
 73             b.n=N;
 74             b.m=a.m;
 75             b.step=a.step+1;
 76             if(check(b)){
 77                 printf("%d
",b.step);
 78                 return;
 79             }
 80             if(!vis[b.m][b.n]){
 81                 vis[b.m][b.n]=1;
 82                 dl.push(b);
 83             }
 84             }
 85             else{
 86                 b.n=a.n+a.s;
 87                 b.s=0;
 88                 b.m=a.m;
 89                 b.step=a.step+1;
 90                 if(check(b)){
 91                 printf("%d
",b.step);
 92                 return;
 93             }
 94             if(!vis[b.m][b.n]){
 95                 vis[b.m][b.n]=1;
 96                 dl.push(b);
 97                 }
 98             }
 99         }
100         if(a.s<S){//m-s
101             if(a.m>=S-a.s){
102             b.m=a.m-(S-a.s);
103             b.s=S;
104             b.n=a.n;
105             b.step=a.step+1;
106             if(check(b)){
107                 printf("%d
",b.step);
108                 return;
109             }
110             if(!vis[b.m][b.n]){
111                 vis[b.m][b.n]=1;
112                 dl.push(b);
113             }
114             }
115             else{
116                 b.s=a.s+a.m;
117                 b.n=a.n;
118                 b.m=0;
119                 b.step=a.step+1;
120                 if(check(b)){
121                 printf("%d
",b.step);
122                 return;
123             }
124             if(!vis[b.m][b.n]){
125                 vis[b.m][b.n]=1;
126                 dl.push(b);
127                 }
128             }
129         }
130         if(a.n<N){//m-n
131             if(a.m>=N-a.n){
132             b.m=a.m-(N-a.n);
133             b.s=a.s;
134             b.n=N;
135             b.step=a.step+1;
136             if(check(b)){
137                 printf("%d
",b.step);
138                 return;
139             }
140             if(!vis[b.m][b.n]){
141                 vis[b.m][b.n]=1;
142                 dl.push(b);
143                 }
144             }
145             else{
146                 b.n=a.n+a.m;
147                 b.s=a.s;
148                 b.m=0;
149                 b.step=a.step+1;
150                 if(check(b)){
151                 printf("%d
",b.step);
152                 return;
153             }
154             if(!vis[b.m][b.n]){
155                 vis[b.m][b.n]=1;
156                 dl.push(b);
157                 }
158             }
159         }
160         if(a.s<S){//n-s
161             if(a.n>=S-a.s){
162             b.n=a.n-(S-a.s);
163             b.s=S;
164             b.m=a.m;
165             b.step=a.step+1;
166             if(check(b)){
167                 printf("%d
",b.step);
168                 return;
169             }
170             if(!vis[b.m][b.n]){
171                 vis[b.m][b.n]=1;
172                 dl.push(b);
173                 }
174             }
175             else{
176                 b.s=a.s+a.n;
177                 b.m=a.m;
178                 b.n=0;
179                 b.step=a.step+1;
180                 if(check(b)){
181                 printf("%d
",b.step);
182                 return;
183             }
184             if(!vis[b.m][b.n]){
185                 vis[b.m][b.n]=1;
186                 dl.push(b);
187                 }
188             }
189         }
190         if(a.m<M){//n-m
191         if(a.n>=M-a.m){
192             b.n=a.n-(M-a.m);
193             b.s=a.s;
194             b.m=M;
195             b.step=a.step+1;
196             if(check(b)){
197                 printf("%d
",b.step);
198                 return;
199             }
200             if(!vis[b.m][b.n]){
201                 vis[b.m][b.n]=1;
202                 dl.push(b);
203             }
204             }
205         else{
206                 b.m=a.m+a.n;
207                 b.s=a.s;
208                 b.n=0;
209                 b.step=a.step+1;
210                 if(check(b)){
211                 printf("%d
",b.step);
212                 return;
213             }
214             if(!vis[b.m][b.n]){
215                 vis[b.m][b.n]=1;
216                 dl.push(b);
217                 }
218             }
219         }
220     }
221     puts("NO");
222 }
223 int main(){
224     while(~scanf("%d%d%d",&S,&M,&N),S|M|N){
225         if(S&1)puts("NO");
226         else bfs();
227     }
228     return 0;}

代码2:

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<cmath>
 5 #include<algorithm>
 6 #include<queue>
 7 using namespace std;
 8 const int INF=0x3f3f3f3f;
 9 int c[3];
10 int vis[110][110];
11 struct Node{
12     int v[3],step;
13 };
14 bool check(Node a){
15     int x,y,z;
16     x=a.v[0];y=a.v[1];z=a.v[2];
17     int t=c[0]/2;
18     //printf("%d %d %d %d
",x,y,z,t);
19     if(x==t&&y==t)return true;
20     if(y==t&&z==t)return true;
21     if(x==t&&z==t)return true;
22     return false;
23 }
24 void bfs(){
25     Node a,b;
26     memset(vis,0,sizeof(vis));
27     queue<Node>dl;
28     a.v[0]=c[0];
29     a.v[1]=0;
30     a.v[2]=0;
31     a.step=0;
32     dl.push(a);
33     vis[a.v[1]][a.v[2]]=1;
34     while(!dl.empty()){
35         a=dl.front();
36         dl.pop();
37         for(int i=0;i<3;i++)
38         for(int j=0;j<3;j++){
39             if(i==j)continue;
40             b=a;
41             b.step=a.step+1;
42             if(b.v[i]>=c[j]-b.v[j]){
43                 b.v[i]=b.v[i]-(c[j]-b.v[j]);
44                 b.v[j]=c[j];
45             }
46             else{
47                 b.v[j]+=b.v[i];
48                 b.v[i]=0;
49             }
50             if(check(b)){
51                     printf("%d
",b.step);return;
52                 }
53                 if(!vis[b.v[1]][b.v[2]]){
54                     dl.push(b);vis[b.v[1]][b.v[2]]=1;
55                 }
56         }
57         }
58     puts("NO");
59 }
60 int main(){
61     while(~scanf("%d%d%d",c,c+1,c+2),c[0]|c[1]|c[2]){
62         if(c[0]&1)puts("NO");
63         else bfs();
64     }
65     return 0;}
原文地址:https://www.cnblogs.com/handsomecui/p/4928084.html