Poj 3414 -- pots -- BFS + backtracking path

Source: Internet
Author: User

Pots
Time limit:1000 ms   Memory limit:65536 K
Total submissions:9963   Accepted:4179   Special Judge

Description

You are given two pots, having the volumeAAndBLiters respectively. The following operations can be saved med:

    1. Fill (I) fill the potI(1 ≤I≤ 2) from the tap;
    2. Drop (I) Empty the potITo the drain;
    3. 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)
 
 
 
 
 
Like a two-dimensional BFS that we have previously done, we only need to backtrack the path. It is very easy to add a variable in the struct to record the subscript of the previous state in the queue (it is better to manually knock the queue, at this time, it seems inconvenient to use the STL queue. Finally, find the reverse print path that meets the condition (because all records are in the previous State)
# Include <cstdio> # include <iostream> # include <cstring> # include <cstdlib> # include <queue> using namespace STD; int M, N, C, S, e, p; typedef struct node {int V1, V2, cur, pre, OP ;}; bool vis [999] [999]; int ans [10010]; node que [10010]; void BFS () {P = 0; S = 0; E = 0; int Pos; node T = {0, 0, 0}; que [E ++] = T; vis [0] [0] = 1; while (S <E) {node F = que [s]; Pos = s; s ++; If (F. v1 = c | f. v2 = c) {printf ("% d \ n", F. OP); int TEM = Pos; For (INT I = 0; I <F. OP; I ++) {Ans [p ++] = que [TEM]. cur; TEM = que [TEM]. pre;} For (INT I = p-1; I> = 0; I --) {Switch (ANS [I]) {Case 1: printf ("fill (1) \ n "); break; Case 2: printf (" fill (2) \ n "); break; Case 3: printf (" drop (1) \ n "); break; Case 4: printf ("drop (2) \ n"); break; Case 5: printf ("Pour (2, 1) \ n"); break; Case 6: printf ("Pour () \ n"); break ;}} return ;}if (F. v1! = M) {T. V1 = m; T. Op = f. OP + 1; T. v2 = f. V2; If (! Vis [T. v1] [T. v2]) {vis [T. v1] [T. v2] = 1; T. cur = 1; T. pre = Pos; que [E ++] = T ;}} if (F. v2! = N) {T. v2 = N; T. Op = f. OP + 1; T. V1 = f. V1; If (! Vis [T. v1] [T. v2]) {vis [T. v1] [T. v2] = 1; T. cur = 2; T. pre = Pos; que [E ++] = T ;}} if (F. v1! = 0) {T. V1 = 0; T. v2 = f. V2; T. Op = f. OP + 1; if (! Vis [T. v1] [T. v2]) {vis [T. v1] [T. v2] = 1; T. cur = 3; T. pre = Pos; que [E ++] = T ;}} if (F. v2! = 0) {T. v2 = 0; T. V1 = f. V1; T. Op = f. OP + 1; if (! Vis [T. v1] [T. v2]) {vis [T. v1] [T. v2] = 1; T. cur = 4; T. pre = Pos; que [E ++] = T ;}} if (F. v2! = 0 & F. V1! = M) {T. v2 = f. v2-(m-f.v1); If (T. v2 <0) T. v2 = 0; T. v1 = f. v1 + F. v2; If (T. v1> m) T. v1 = m; T. OP = f. OP + 1; if (! Vis [T. v1] [T. v2]) {vis [T. v1] [T. v2] = 1; T. cur = 5; T. pre = Pos; que [E ++] = T ;}} if (F. v1! = 0 & F. V2! = N) {T. v1 = f. v1-(n-f.v2); If (T. v1 <0) T. v1 = 0; T. v2 = f. v2 + F. v1; If (T. v2> N) T. v2 = N; T. OP = f. OP + 1; if (! Vis [T. v1] [T. v2]) {vis [T. v1] [T. v2] = 1; T. cur = 6; T. pre = Pos; que [E ++] = T ;}} puts ("impossible");} int main () {While (CIN> m> N> C) {memset (VIS, 0, sizeof (VIS); BFS () ;}return 0 ;}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.