Topic Portal
1 /*2 BFS: Pour water problem, when C is odd, no solution. A total of six cases, as long as the conditions in line with the team, I wrote when the state vised continue3 results found a half-day only to find the bug, tears .... (It's hard to find a good one on the internet.)4 */5 /************************************************6 Author:running_time7 Created time:2015-8-4 10:54:168 File Name:HDOJ_1495.cpp9 ************************************************/Ten One#include <cstdio> A#include <algorithm> -#include <iostream> -#include <sstream> the#include <cstring> -#include <cmath> -#include <string> -#include <vector> +#include <queue> -#include <deque> +#include <stack> A#include <list> at#include <map> -#include <Set> -#include <bitset> -#include <cstdlib> -#include <ctime> - using namespacestd; in - #defineLson L, Mid, RT << 1 to #defineRson mid + 1, R, RT << 1 | 1 +typedefLong Longll; - Const intMAXN = 1e2 +Ten; the Const intINF =0x3f3f3f3f; * Const intMOD = 1e9 +7; $ structCola {Panax Notoginseng intA, B, C, step; - }; the BOOLVIS[MAXN][MAXN]; + intA, B, C; A the voidBFS (void) { +memset (Vis,false,sizeof(Vis)); -Queue<cola> Q; Q.push ((Cola) {0,0C0}); vis[0][0] =true; $ while(!Q.empty ()) { $Cola C =Q.front (); Q.pop (); - if(C.A = = C.b && C.A = = c/2) || (C.A = = C.C && C.A = = c/2) || (c.b = = C.C && C.B = = c/2)) { -printf ("%d\n", C.step);return ; the } - if(C.c >0) {//C-aWuyi intt = min (c.c, A-C.A); the if(!vis[c.a+T] [C.b]) { -VIS[C.A+T][C.B] =true; Q.push ((Cola) {c.a+t, c.b, c.c-t, C.step +1}); Wu } - } About if(C.c >0) {//C-B $ intt = min (c.c, B-c.b); - if(!vis[c.a][c.b+T]) { -VIS[C.A][C.B+T] =true; Q.push ((Cola) {c.a, c.b+t, c.c-t, C.step +1}); - } A } + if(C.a >0) {//A-c the intt = min (C.A, C-c.c); - if(!vis[c.a-T] [C.b]) { $VIS[C.A-T][C.B] =true; Q.push ((Cola) {c.a-t, c.b, c.c+t, C.step +1}); the } the } the if(C.b >0) {//B-C the intt = min (c.b, C-c.c); - if(!vis[c.a][c.b-T]) { inVIS[C.A][C.B-T] =true; Q.push ((Cola) {c.a, c.b-t, c.c+t, C.step +1}); the } the } About if(C.a >0) {//A-B the intt = min (C.A, B-c.b); the if(!vis[c.a-t][c.b+T]) { theVIS[C.A-T][C.B+T] =true; Q.push ((Cola) {c.a-t, c.b+t, C.C, C.step +1}); + } - } the if(C.b >0) {//B-aBayi intt = min (c.b, A-C.A); the if(!vis[c.a+t][c.b-T]) { theVIS[C.A+T][C.B-T] =true; Q.push ((Cola) {c.a+t, c.b-t, C.C, C.step +1}); - } - } the } the thePuts ("NO"); the } - the intMainvoid) {//HDU 1495 very coke the while(SCANF ("%d%d%d", &c, &a, &b) = =3) { the if(! C &&! A &&! B Break;94 if(C &1) { thePuts ("NO");Continue; the } the BFS ();98 } About - return 0;101}
BFS (pour water problem) HDU 1495 very coke