Description
You are given two pots, having the volumeAAndBLiters respectively. The following operations can be saved med:
- Fill (I) fill the potI(1 ≤I≤ 2) from the tap;
- Drop (I) Empty the potITo the drain;
- Pour (I, j) pour from potITo potJ; After this operation either the potJIs full (and there may be some water left in the potI), Or the potIIs empty (and all its contents have been moved to the potJ).
Write a program to find the shortest possible sequence of these operations that will yield exactlyCLiters of water in one of the pots.
Input
On the first and only line are the numbersA,B, AndC. These are all integers in the range from 1 to 100 andC≤ Max (A,B).
Output
The first line of the output must contain the length of the sequence of operationsK. The followingKLines must each describe one operation. if there are several sequences of minimal length, output any one of them. if the desired result can't be 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)There are two empty bottles a, B are their capacity, C is the capacity target, three operations can be filled with any bottle, empty any bottle, pour any bottle into another bottle (which can be left but cannot overflow); find the minimum number of operations required for the volume of any bottle to reach the target volume, and output the operation accordingly.Code:# Include <iostream> # include <cstdio> # include <queue> # include <cstring> using namespace STD; int m, n, k; int vis [105] [105]; char OPR [20] [20] = {"", "fill (1)", "fill (2)", "Drop (1)", "Drop (2) "," Pour (200) "," Pour () "}; // six operations are performed on struct node {int X, Y, step; int W []; // array used to record the path}; void BFS () {int I, j; int kx, ky; memset (VIS, 0, sizeof (VIS )); queue <node> q; node now, next; now. X = 0, now. y = 0, now. step = 0; q. push (Now); vis [0] [0] = 1; while (! Q. empty () {now = Q. front (); q. pop (); If (now. X = k | now. y = k) {cout <now. step <Endl; for (I = 1; I <= now. step; I ++) {cout <OPR [now. W [I] <Endl;} return ;}for (I = 0; I <6; I ++) // six operations in total {if (I = 0) {next. y = now. y; next. X = m; now. W [now. step + 1] = 1; // update at any time} else if (I = 1) {next. X = now. x; next. y = N; now. W [now. step + 1] = 2;} else if (I = 2) {next. y = now. y; next. X = 0; now. W [now. step + 1] = 3;} else if (I = 3) {next. X = now. x; next. y = 0; now. W [now. step + 1] = 4;} else if (I = 4) {If (n-now.y> now. x) // each pour should have two cases {next. X = 0; next. y = now. X + now. y;} else {next. y = N; next. X = now. x-N + now. y;} now. W [now. step + 1] = 5;} else if (I = 5) {If (m-now.x> now. y) {next. y = 0; next. X = now. X + now. y;} else {next. X = m; next. y = now. y-M + now. x;} now. W [now. step + 1] = 6;} If (vis [next. x] [next. y] = 1) continue; vis [next. x] [next. y] = 1; next. step = now. step + 1; for (j = 1; j <= next. step; j ++) // record previous actions {next. W [J] = now. W [J];} Q. push (next) ;}}cout <"impossible" <Endl; // do not forget this situation return;} int main () {int I; while (CIN> m> N> K) {BFS ();} return 0 ;}
I am still happy to write the code successfully through my understanding of the queue...
Poj 3414 wide search of pots record paths