I-pots
Time Limit:1000MS
Memory Limit:65536KB
64bit IO Format:%I64D &%i64u Submit Status
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 may be some water left in the pot I), or the PO T 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 one 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)
According to the requirements of the problem, direct BFS can be
#include <iostream> #include <string> #include <cstdio> #include <cstring> #include <
Algorithm> #include <queue> using namespace std;
const int MAXN = 100 + 5;
int A, B, C;
BOOL VIS[MAXN][MAXN];
struct O {int A, B;
String str;
O () {} o (int a, int b, string str): A (a), B (b), str (str) {}};
Queue<o> Q;
String X[6] = {"Fill (1)", "Fill (2)", "Drop (1)", "Drop (2)", "pour", "pour (2,1)"};
String Z[6] = {"0", "1", "2", "3", "4", "5"};
BOOL BFS () {memset (Vis, false, sizeof (VIS));
Q.push (o (0, 0, "")); while (!
Q.empty ()) {o e = Q.front ();
Q.pop ();
if (vis[e.a][e.b]) continue;
VIS[E.A][E.B] = true;
if (e.a = = c | | e.b = = c) {int len = e.str.length ();
printf ("%d\n", Len);
for (int i = 0; i < len; i + +) {cout << x[e.str[i]-' 0 '] << Endl;
} return true; } for (int i = 0; i < 6;i + +) {//6 processing can be o h = e;
switch (i) {case 0:h.a = A;
Break
Case 1:h.b = b;
Break
Case 2:H.A = 0;
Break
Case 3:h.b = 0;
Break
Case 4:if (e.b + e.a < b) {h.b = e.b + e.a;
H.A = 0;
} else {h.b = b;
H.A = e.b + e.a-b;
} break;
Case 5:if (e.b + E.a < a) {H.A = E.a + e.b;
h.b = 0;
} else {h.a = A;
H.B = e.b + e.a-a;
} break;
} h.str = E.str + z[i];
Q.push (h);
}} return false; } int main () {while (~SCANF ("%d%D%d ", &a, &b, &c)) {bool i = BFS ();
if (!i) {printf ("impossible\n");
}} return 0;
}