Poj 3414 pots [BFS simulated water dumping problem]

Source: Internet
Author: User
Link: http://poj.org/problem? Id = 3414 http://acm.hust.edu.cn/vjudge/contest/view.action? Cid = 22009 # Problem/jpots
Time limit:1000 ms   Memory limit:65536 K
Total submissions:8253   Accepted:3499   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
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)

Source

Northeastern Europe 2002, western subregion

Algorithm: Simulate BFS in an array. Question: Let's ask two containers a B if they can pour water through a limited number of steps to get the minimum number of water output steps with a capacity of C, and output the operations in each step at the same time. If the target status cannot be reached, the outputImpossibleIdeas:

There are only so many statuses in total, and there are only six significant BFS operations after each step. The key is to record the path of each step. I started to use the BFS + container and found that the output path could not be properly handled. I had to re-start the array simulation. Although the container is easy to use and convenient, the efficiency cannot be compared with the array simulation without considering the memory.
Note that the range of A and B is an integer from 1 to 100, then we can use VIS [I] [J] to record each State 0 <= I, j <= 100; I indicates the current water in container A, and J indicates the current water in container B.
It should be easy to separate the analysis. There are only six next steps corresponding to each State: 1. Fill the pool with water a 2: Fill the pool with water B 3: pour all the water in a into the waste pool 4: pour all the water in B into the waste pool 5: pour the water in a into B [cannot overflow] There may be two corresponding states: k1 indicates the water in a, K2 indicates the water in B. If k1 + K2 <= B, K2 = k1 + K2; k1 = 0 [not enough to fill the container B] Pay Attention to the order of otherwise k1 = k1 + k2-B; k2 = B [can fill the container B] Six: if the water in B is poured into a [cannot overflow], there are two situations. The analysis is the same as above. If k1 + K2 <= A, k1 = k1 + K2; k2 = 0; otherwise k2 = k1 + k2-A; k1 =
The struct array is used to simulate the queue. K1 and K2 are used to record the state of water in the current container. Previously, there are only 6 operations corresponding to each situation, in this case, the Operation records corresponding to each case are processed when the output is 1 to 6. Of course, the minimum number of steps used to record the current State is required. Because the path is to be recorded, a pre is defined to record the subscript of the previous step in the array simulation queue.
Finally, if the goal can be achieved, when determining the last step, record the number lastindex In the last step in the array, and then find the number in the array from the lastindex. [pre] the previous step contains the ID in the array [] and then follow the scanned path can be traversed in sequence.
Code:

3414 Accepted 228 K 0 ms C ++ 4165b

# Include <stdio. h> # include <string. h> const int maxn = 110; int vis [maxn] [maxn]; // mark whether the status has crossed int A, B, C; // container size int step; // The final number of steps int flag; // whether the record can be successful/* status record */struct status {int K1, K2; // the status of the current water int op; // int step of the current operation; // int pre of the number of record steps; // subscript of the previous record} Q [maxn * maxn]; int ID [maxn * maxn]; // record the number of the final operation in the queue int lastindex; // the last number void BFS () {status now, next; int head, tail; head = tail = 0; Q [tail]. k1 = 0; Q [tail]. k2 = 0; Q [tail]. OP = 0; Q [tail]. step = 0; Q [tail]. pre = 0; tail ++; memset (VIS, 0, sizeof (VIS); vis [0] [0] = 1; // mark that the initial status has been queued. While (Head <tail) // when the queue is not empty {now = Q [head]; // retrieve the first head of the queue ++; // If (now. k1 = c | now. k2 = C) // This should not happen, c = 0 {flag = 1; step = now. step; lastindex = head-1; // record the number of the last step} For (INT I = 1; I <= 6; I ++) // traverse 6 cases respectively {if (I = 1) // fill (1) {next. k1 = A; next. k2 = now. k2;} else If (I = 2) // fill (2) {next. k1 = now. k1; next. k2 = B;} else if (I = 3) // drop (1) {next. k1 = 0; next. k2 = now. k2;} else if (I = 4) // drop (2); {next. k1 = now. k1; next. k2 = 0;} else if (I = 5) // pour (1, 2) {If (now. k1 + now. k2 <= B) // if it cannot be filled with B {next. k1 = 0; next. k2 = now. k1 + now. k2;} else // if it can be filled with B {next. k1 = now. k1 + now. k2-b; next. k2 = B ;}} else if (I = 6) // pour (2, 1) {If (now. k1 + now. k2 <= A) // if it cannot be filled with a {next. k1 = now. k1 + now. k2; next. k2 = 0;} else // if it can be filled with B {next. k1 = A; next. k2 = now. k1 + now. k2-a;} next. OP = I; // record the operation if (! Vis [next. k1] [next. k2]) // if the current status has not been queued {vis [next. k1] [next. k2] = 1; // mark the current status to join next. step = now. step + 1; // number of steps + 1 next. pre = head-1; // record the number of the previous step // Q. push (next); // Q [tail] = next; Join team end Q [tail]. k1 = next. k1; Q [tail]. k2 = next. k2; Q [tail]. OP = next. OP; Q [tail]. step = next. step; Q [tail]. pre = next. pre; tail ++; // If (next. k1 = c | next. k2 = C) // if the target State is reached {flag = 1; // mark success step = next. step; // record the total number of steps la Stindex = tail-1; // record the number of the last step in the simulated array: Return ;}}} int main () {While (scanf ("% d", & A, & B, & C )! = EOF) {flag = 0; // initialization fails step = 0; BFS (); If (FLAG) {printf ("% d \ n", step ); id [STEP] = lastindex; // The number of the last step in the simulated array for (INT I = Step-1; I> = 1; I --) {ID [I] = Q [ID [I + 1]. pre; // find the number of the previous step in the simulated array} For (INT I = 1; I <= step; I ++) {If (Q [ID [I]. OP = 1) printf ("fill (1) \ n"); else if (Q [ID [I]. OP = 2) printf ("fill (2) \ n"); else if (Q [ID [I]. OP = 3) printf ("drop (1) \ n"); else if (Q [ID [I]. OP = 4) printf ("drop (2) \ n"); else if (Q [ID [I]. OP = 5) printf ("Pour (1, 2) \ n"); else if (Q [ID [I]. OP = 6) printf ("Pour () \ n") ;}} else printf ("impossible \ n") ;}return 0 ;}
Summary:

In fact, this question was the last senior student in the first semester of the freshman year.
After two years, I had no idea until a few days ago. Yesterday I thought I had to practice the Basic Search and try again. After careful analysis, the question was still very simple, sometimes it is too impetuous.
After completing this, 1a found that this question was actually the 100th question of AC directly on poj. After two years, it only broke today, and the number of blogs was catching up with the number of questions. It is really worth reflecting on. Sometimes a lot of things can be done only by refreshing questions, so I have been so weak for the past two years. Now I can only create new questions, there is a reason why multiple HDU schools are completely different...
Then and kuangbin God spit out the next, he and I said, and his team brush more than one individual school, two months on the brush 2000 + straight jump poj eighth http://poj.org/userstatus? User_id = neko13
Although it is obviously not scientific, it also proves that people are willing to make a lot of questions. It's crazy. If we're not crazy, we're all old.

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.