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