POJ---3414---Pots

Source: Internet
Author: User

Title Link: http://poj.org/problem?id=3414

Pots
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 14306 Accepted: 6033 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.   i  to the drain;
  3. pour (i,j)     pour from pot< Span class= "Apple-converted-space" >&NBSP; i  to Pot&NBSP; J ; After this operation either the Pot&NBSP; J  is Full (and there is some water left in the Pot&nbs P i ), or the Pot&NBSP; i  is 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 Lite RS 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'. /c6>

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
#include <cstdio>#include<cstring>#include<iostream>#include<cmath>#include<vector>#include<queue>#include<algorithm>using namespaceStd;typedefLong LongLL;Const intmaxn=107;Const intinf=0x3f3f3f3f;intA, B, C, F;intVIS[MAXN][MAXN];Charstr[6][Ten]= {"FILL (1)","FILL (2)","pour (ON)","pour (2,1)","DROP (1)","DROP (2)"};///steps corresponding to the actionstructnode{intx, y, step; CharS[MAXN];};voidBFs () {memset (Vis,0,sizeof(VIS));    node p, q; Queue<node>p; P.x=0; P.Y=0; P.step=0; p.s[0]='0';    Q.push (P); VIS[P.X][P.Y]=1;  while(!Q.empty ()) {Q=Q.front ();        Q.pop (); if(Q.x==c | | q.y==c) {f=1; printf ("%d\n", Q.step);  for(intI=1; i<=q.step; i++) printf ("%s\n", str[q.s[i]-'0']); return ; }         for(intI=0; i<6; i++) {P.step=q.step+1;///record your run to the first few stepsp.x=p.y=0;            strcpy (P.S, Q.S); P.s[p.step]=i+'0';///Step q.step steps to select            if(i==0&AMP;&AMP;Q.X!=A)///fill the first cup, then the water in this cup must be dissatisfied{p.x=A; P.Y=q.y; }            Else if(i==1&AMP;&AMP;Q.Y!=B)///fill the Second Cup, then the water in this cup must be dissatisfied{p.x=q.x; P.Y=b; }            Else if(i==2&AMP;&AMP;Q.X&AMP;&AMP;Q.Y!=B)///1 to 2 Plus, then to ensure that 1 must have water, 2 must be dissatisfied in order to add            {                if(q.x+q.y<=b)///1 can all be added to the 2{p.x=0; P.Y=q.x+q.y; }                Else///not all of them, just the rest.{p.x=q.x+q.y-b; P.Y=b; }            }            Else if(i==3&AMP;&AMP;Q.Y&AMP;&AMP;Q.X!=A)///2 to 1 Plus, then to ensure that 2 must have water, 1 must be dissatisfied in order to add            {                if(q.x+q.y<=a)///2 can all be added to the 1{p.x=q.x+q.y; P.Y=0; }                Else///not all of them, just the rest.{p.x=A; P.Y=q.x+q.y-A; }            }            Else if(i==4&& q.x)///will be 1 clear 0, then 1 must not be equal to 0{p.x=0; P.Y=q.y; }            Else if(i==5&&q.y) {p.x=q.x; P.Y=0; }            if(!vis[p.x][p.y])///no sign of the queue{VIS[P.X][P.Y]=1;            Q.push (P); }        }    }}intMain () { while(~SCANF (" %d%d%d", &a, &b, &c)) {f=0;        BFS (); if(!f) puts ("Impossible"); }    return 0;}

POJ---3414---Pots

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.