Description
You are given the pots of the volume of A and B liters respectively. The following operations can be performed:
Fill (i) Fill the pot I (1≤i≤2) from the tap;
DROP (i) empty the pot I to the drain;
Pour (i,j) pour from pot I to pot J; After this operation either the pot J was full (and there could be some water left in the pot I), or the pot I was empty (and All its contents has been moved to the pot J).
Write a program to find the shortest possible sequence of these operations that'll yield exactly C liters of water in on E of the pots.
Input
On the first and only line is the numbers A, B, and C. These is all integers in the range from 1 to C≤max (A, B).
Output
The first line of the output must contain the length of the sequence of operations K. The following K lines must each describe one operation. If There is several sequences of minimal length, output any one of them. If the desired result can ' t is achieved, the first and only line of the file must contain the word ' impossible '.
Sample Input
3 5 4
Sample Output
6
FILL (2)
Pour (2,1)
DROP (1)
Pour (2,1)
FILL (2)
Pour (2,1)
Simple BFS, three operations in a total of six states, traversing on the line
#include <iostream>#include <cstring>#include <string>#include <vector>#include <queue>#include <map>#include <cstdio>#include <algorithm>#define INF 0x3f3f3f3f#define MAXN 1010Using namespaceSTD;struct node{int, b;int op[10000],cnt;};BOOL Flag;int vis[ the][ the];void BFs (Node start,int a,int b,int c) {queue<node> q;Q. Push(start);while (!q. Empty()) {Node tmp=q. Front();Q. Pop();Cout<<tmp. A<<" "<<tmp. b<<endl;if (TMP. A==c| | Tmp. b==c) {printf ("%d\n"Tmp. CNT);for (int i=0; i<tmp.cnt; ++i){if (TMP. Op[i]== One) printf ("FILL (1) \ n");if (TMP. Op[i]== A) printf ("FILL (2) \ n");if (TMP. Op[i]== +) printf ("DROP (1) \ n");if (TMP. Op[i]== A) printf ("DROP (2) \ n");if (TMP. Op[i]==312) printf ("pour \ n");if (TMP. Op[i]==321) printf ("pour (2,1) \ n");} flag=true;Return;} Node TMP1;Tmp1=tmp;if (TMP1. A!=0) {TMP1. A=0;Tmp1. Op[TMP1. CNT++]= +;if (!VIS[TMP1. A][tmp1. b]) {VIS[TMP1. A][tmp1. b]=1;Q. Push(TMP1);}} tmp1=tmp;if (TMP1. b!=0) {TMP1. b=0;Tmp1. Op[TMP1. CNT++]= A;if (!VIS[TMP1. A][tmp1. b]) {VIS[TMP1. A][tmp1. b]=1;Q. Push(TMP1);}} tmp1=tmp;if (TMP1. A!=a) {TMP1. A=a;Tmp1. Op[TMP1. CNT++]= One;if (!VIS[TMP1. A][tmp1. b]) {VIS[TMP1. A][tmp1. b]=1;Q. Push(TMP1);}} tmp1=tmp;if (TMP1. b!=B) {TMP1. b=b;Tmp1. Op[TMP1. CNT++]= A;if (!VIS[TMP1. A][tmp1. b]) {VIS[TMP1. A][tmp1. b]=1;Q. Push(TMP1);}} tmp1=tmp;if (TMP1. A>b-tmp1. b) {TMP1. A-= (B-TMP1. b);Tmp1. b=b;Tmp1. Op[TMP1. CNT++]=312;} else {TMP1. b+=tmp1. A;Tmp1. A=0;Tmp1. Op[TMP1. CNT++]=312;} if (!VIS[TMP1. A][tmp1. b]) {VIS[TMP1. A][tmp1. b]=1;Q. Push(TMP1);} tmp1=tmp;if (TMP1. b>a-tmp1. A) {TMP1. b-= (A-TMP1. A);Tmp1. A=a;Tmp1. Op[TMP1. CNT++]=321;} else {TMP1. A+=tmp1. b;Tmp1. b=0;Tmp1. Op[TMP1. CNT++]=321;} if (!VIS[TMP1. A][tmp1. b]) {VIS[TMP1. A][tmp1. b]=1;Q. Push(TMP1);}}}int Main () {int a,b,c;while (~SCANF ("%d%d%d", &a,&b,&c)) {Flag=false;Node start;memset (Vis,0, sizeof (VIS));Start. A=0;Start. b=0;Start. CNT=0;BFS (START,A,B,C);if (!flag) cout<<"Impossible"<<endl;} return0;}
Poj3414--pots (BFS)