Pots
| Time limit:1000 ms |
|
Memory limit:65536 K |
| Total submissions:10071 |
|
Accepted:4237 |
|
Special Judge |
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
6FILL(2)POUR(2,1)DROP(1)POUR(2,1)FILL(2)POUR(2,1)
Idea: there are six operations: pour out the water in a, fill a, and pour the water in B into a; B is similar to.
The maximum volume of the jar is 100, and a constant n = 100 is set to open a two-dimensional array to record the state change value.
1. Add t to a from the faucet, record-t,
2. Add t to B from the tap, record-T-N,
3. Add water t to afrom B and record t
4. Add t to B from a and record N + T
5. Drop the water in a and record 2 * n + T (A's original water t)
6. Drop B's water and record 3 * n + T (B's original water t)
# Include <stdio. h> # include <queue> # include <map> # include <string. h> using namespace STD; # define N 105 const int INF = 0x1f1f1f1f; int A, B, C, flag; int mark [N] [N]; struct node {int X, y, T; friend bool operator <(node A, Node B) {return. t> B. t ;}}; void prif (int x, int y) // recursive output path {If (x = 0 & Y = 0) return; if (MARK [x] [Y]> 3 * n) {prif (x, Mark [x] [Y]-3 * n); printf ("drop (2) \ n ");} else if (MARK [x] [Y]> 2 * n) {P Rif (MARK [x] [Y]-2 * n, Y); printf ("drop (1) \ n ");} else if (MARK [x] [Y]> N) {int TMP = mark [x] [Y]-N; prif (x + TMP, Y-TMP ); printf ("Pour (1, 2) \ n");} else if (MARK [x] [Y]> 0) {int TMP = mark [x] [Y]; prif (X-TMP, Y + TMP); printf ("Pour (2, 1) \ n");} else if (MARK [x] [Y]>-N) {int TMP =-MARK [x] [Y]; prif (X-TMP, Y); printf ("fill (1) \ n ");} else if (MARK [x] [Y] <-N) {int TMP = N + Mark [x] [Y]; prif (X, Y + TMP ); printf ("fill (2) \ n") ;}} void BF S () {priority_queue <node> q; node cur, next; Mark [0] [0] = inf; // This status can only appear once, assign a value to INF to prevent interference with other values. MARK [a] [0] =-A; Mark [0] [B] =-B-N; cur. T = 1; cur. X = A; cur. y = 0; q. push (cur); cur. X = 0; cur. y = B; q. push (cur); While (! Q. empty () {cur = Q. top (); q. pop (); next. T = cur. t + 1; if (cur. X = c | cur. y = c) {flag = 1; printf ("% d \ n", cur. t); prif (cur. x, cur. y); return;} If (cur. x <A) // add water to a {int TMP = a-cur.x; next. y = cur. y; next. X = A; // If (! Mark [next. x] [next. y]) {mark [next. x] [next. y] =-TMP; q. push (next);} If (cur. y> 0) // The water from B {int TMP = min (cur. y, a-cur.x); next. X = cur. X + TMP; next. y = cur. y-TMP; If (! Mark [next. x] [next. y]) {mark [next. x] [next. y] = TMP; q. push (next) ;}} if (cur. Y <B) // Add to B {int TMP = b-cur.y; next. X = cur. x; next. y = B; // If (! Mark [next. x] [next. y]) {mark [next. x] [next. y] =-TMP-N; q. push (next);} If (cur. x> 0) // The water from a {int TMP = min (cur. x, b-cur.y); next. y = cur. Y + TMP; next. X = cur. x-TMP; If (! Mark [next. x] [next. y]) {mark [next. x] [next. y] = TMP + N; q. push (next) ;}} if (cur. x> 0) // drop the water {int TMP = cur. x; next. X = 0; next. y = cur. y; If (! Mark [next. x] [next. y]) {mark [next. x] [next. y] = 2 * n + TMP; q. push (next) ;}} if (cur. y> 0) {int TMP = cur. y; next. y = 0; next. X = cur. x; If (! Mark [next. x] [next. y]) {mark [next. x] [next. y] = 3 * n + TMP; q. push (next) ;}}} int main () {While (scanf ("% d", & A, & B, & C )! =-1) {memset (mark, 0, sizeof (Mark); flag = 0; BFS (); If (! Flag) printf ("impossible \ n");} return 0 ;}
Poj 3414 pots (BFS + path record)