Title Address: POJ 3414
Test instructions: Give you A, B two container capacity, six methods of operation, ask at least how many times can make one of the containers of water reach volume C, if not, then output impossible.
Train of thought: a total of 6 methods of operation, 0: Full of a,1: pour full b,2: Pour the water in A to B (two cases B is filled with a has surplus or a end), 3: The water in B is inverted to a (similar to the two cases of 2), 4: Empty A, 5: Empty B. Then use Vis[i][j] to record the number of steps when a capacity of I,b for J, Op[i][j][k] records when a's capacity for the capacity of i,b for the K-step operation is who. Then it's just a mess.
#include <stdio.h>#include <math.h>#include <string.h>#include <stdlib.h>#include <iostream>#include <sstream>#include <algorithm>#include <set>#include <queue>#include <stack>#include <map>#pragma COMMENT (linker, "/stack:102400000,102400000")using namespace STD;typedef__int64 LL;Const intinf=0x3f3f3f3f;Const DoublePi=ACOs(-1.0);Const Doubleesp=1e-7;Const intmaxn= the;structNode {intx, y;} F1,F2;intVIS[MAXN][MAXN];intOP[MAXN][MAXN][MAXN];intA,b,c;voidOperator (inti) {Switch(i) { Case 0:printf("FILL (1) \ n"); Break; Case 1:printf("FILL (2) \ n"); Break; Case 2:printf("pour \ n"); Break; Case 3:printf("pour (2,1) \ n"); Break; Case 4:printf("DROP (1) \ n"); Break; Case 5:printf("DROP (2) \ n"); Break; }}voidBFS () {intI,j; queue<node >Qmemset(vis,-1,sizeof(VIS)); vis[0][0]=0; f1.x=0; f1.y=0; Q.push (F1); while(!q.empty ()) {F1=q.front (); Q.pop ();if(f1.x==c| | F1.Y==C) {printf("%d\n", Vis[f1.x][f1.y]); for(i=0; i<vis[f1.x][f1.y]; i++) {Operator (op[f1.x][f1.y][i]); }return; } for(i=0; i<6; i++) {if(i==0) {f2.x=a; F2.Y=F1.Y; }if(i==1) {f2.x=f1.x; F2.y=b; }if(i==2) {f2.y = f1.x + f1.y;if(f2.y>=b) {f2.x=f2.y-b; F2.y=b; }Elsef2.x=0; }if(i==3) {f2.x=f1.y+f1.x;if(f2.x>=a) {f2.y=f2.x-a; F2.x=a; }Elsef2.y=0; }if(i==4) {f2.x=0; F2.Y=F1.Y; }if(i==5) {f2.x=f1.x; f2.y=0; }if(vis[f2.x][f2.y]==-1) {vis[f2.x][f2.y]=vis[f1.x][f1.y]+1; for(j=0; j<vis[f1.x][f1.y]; J + +) Op[f2.x][f2.y][j]=op[f1.x][f1.y][j]; Op[f2.x][f2.y][vis[f1.x][f1.y]]=i; Q.push (F2); } } }puts("Impossible");}intMain () { while(~scanf("%d%d%d", &a,&b,&c)) {BFS (); }return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
POJ 3414-pots (BFS)