POJ 3414 Pots (bfs+ path record)

Source: Internet
Author: User

Pots
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 11703 Accepted: 4955 Special Judge

Description

You are given the pots of the volume of A and B liters respectively. The following operations can be performed:

    1. Fill (i) Fill the pot i (1≤ i ≤ 2) from the tap;
    2. DROP (i) empty the pot I to the drain;
    3. 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

6FILL (2) pour (2,1) DROP (1) pour (2,1) FILL (2) pour (2,1)

It's cool, once again AC

/************************************************************************* > File name:code/2015summer/ Searching/h.cpp > Author:111qqz > Email: [email protected] > Created time:2015 July 27 Monday 09:11 28 seconds * ***********************************************************************/#include<iostream>#include<iomanip>#include<cstdio>#include<algorithm>#include<cmath>#include<cstring>#include<string>#include<map>#include<Set>#include<queue>#include<vector>#include<stack>#defineY0 ABC111QQZ#defineY1 HUST111QQZ#defineYn hez111qqz#defineJ1 CUTE111QQZ#defineTM CRAZY111QQZ#defineLR DYING111QQZusing namespacestd;#defineREP (i, n) for (int i=0;i<int (n); ++i)typedefLong Longll;typedef unsignedLong LongULL;Const intn=1e2+5;inta,b,c;intD[n][n];BOOLFlag;structnode{intD,opt,par,prea,preb;} Q[n][n];voidPrintintXinty) { //cout<< "x:" <<x<< "y:" <<y<<endl;    if(q[x][y].prea!=-1&&q[x][y].preb!=-1)    {//cout<< "Who is 111qqz" <<endl;print (Q[X][Y].PREA,Q[X][Y].PREB); if(q[x][y].opt==1) {printf ("FILL (%d) \ n", Q[x][y].par); }      if(q[x][y].opt==2) {printf ("DROP (%d) \ n", Q[x][y].par); }      if(q[x][y].opt==3) {printf ("pour (%d,%d) \ n", Q[x][y].par,3-Q[x][y].par); }    }}voidBFs () {memset (q,-1,sizeof(q)); Queue<int>A; Queue<int>b; A.push (0); B.push (0); q[0][0].d=0;  while(!a.empty () &&!B.empty ()) {      intAV =A.front (); A.pop (); intBV =B.front (); B.pop ();//cout<< "av:" <<av<< "BV:" <<bv<<endl;      if(av==c| | bv==C) {flag=true; //cout<< "yeah~~~~~~~~~~~~~~~" <<endl;cout<<q[av][bv].d<<Endl;//cout<< "Prea:" <<q[av][bv].prea<< "Preb:" <<q[av][bv].preb<<endl;print (AV,BV); return; }      if(av<a&&q[a][bv].d==-1) {Q[A][BV].D=q[av][bv].d+1; Q[a][bv].opt=1; Q[a][bv].par=1; Q[a][bv].prea=av; Q[a][bv].preb=BV;        A.push (A);      B.push (BV); }      if(av>0&&q[0][bv].d==-1) {q[0][bv].d=q[av][bv].d+1; q[0][bv].opt=2; q[0][bv].par=1; q[0][bv].prea=av; q[0][bv].preb=BV; A.push (0);      B.push (BV); }      if(bv<b&&q[av][b].d==-1) {Q[AV][B].D=q[av][bv].d+1; Q[av][b].opt=1; Q[av][b].par=2; Q[av][b].prea=av; Q[av][b].preb=BV;        A.push (AV);              B.push (B); }      if(bv>0&&q[av][0].d==-1) {q[av][0].d=q[av][bv].d+1; q[av][0].opt=2; q[av][0].par=2; q[av][0].prea=av; q[av][0].preb=v;        A.push (AV); B.push (0); }      if(av+bv<=b&&q[0][av+bv].d==-1) {q[0][av+bv].d=q[av][bv].d+1; q[0][av+bv].opt=3; q[0][av+bv].par=1; q[0][av+bv].prea=av; q[0][av+bv].preb=BV; A.push (0); B.push (AV+BV); }      if(av+bv>b&&q[av-(B-BV)][b].d==-1)//two cases of pouring 1 into 2      {              intTMP = av-(b-BV); Q[TMP][B].D=q[av][bv].d+1; Q[tmp][b].opt=3; Q[tmp][b].par=1; Q[tmp][b].prea=av; Q[tmp][b].preb=v;        A.push (TMP);      B.push (B); }      if(bv+av<=a&&q[av+bv][0].d==-1) {Q[av+bv][0].d=q[av][bv].d+1; Q[av+bv][0].opt=3; Q[av+bv][0].par=2; Q[av+bv][0].prea=av; Q[av+bv][0].preb=v; A.push (AV+BV); B.push (0); }      if(bv+av>a&&q[a][bv-(A-AV)].d==-1)      {        intTMP = bv-(A-av); Q[A][TMP].D=q[av][bv].d+1; Q[a][tmp].opt=3; Q[a][tmp].par=2; Q[a][tmp].prea=av; Q[a][tmp].preb=BV;        A.push (A);      B.push (TMP); }    }}intMain () {flag=false; CIN>>A>>B>>C;    BFS (); if(!flag) {cout<<"Impossible"<<Endl; }            return 0;}

POJ 3414 Pots (bfs+ path record)

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.