Pots
Time Limit: 1000MS |
|
Memory Limit: 65536K |
Total Submissions: 10071 |
|
Accepted: 4237 |
|
Special Judge |
Description
You are given the pots of the volume of A and B liters respectively. The following operations can be performed:
- Fill (i) Fill the pot i (1≤ i ≤ 2) from the tap;
- DROP (i) empty the pot I to the drain;
- 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)
Idea: A common have 6 kinds of operation: Pour a in the water, put a full, the water in B pour into a. B and A are similar.
The jar has a maximum capacity of 100, set a constant n=100, and open a two-dimensional array to record the value of the state change.
1, from the faucet to a Riga water T, record-T,
2, from the faucet to B Riga water T, record-t-n,
3, from B inside to a plus water T, record t
4, from a inside to B plus water T. Record N+t
5. Pour the water in a, record 2*n+t, (a original water t)
6, the water is poured in B, record 3*n+t, (b original Water t)
#include <stdio.h> #include <queue> #include <map> #include <string> #include <string.h> using namespace std; #define N 105const 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 a.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) {prif (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 () \ 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 BFs () {priority_queue<node>q; Node Cur,next; Mark[0][0]=inf; This state can only occur once. The assignment is 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)//to a plus water {int tmp=a-cur.x; NEXT.Y=CUR.Y; Next.x=a; Water from the faucet if (!mark[next.x][next.y]) {mark[next.x][next.y]=-tmp; Q.push (next); } if (cur.y>0)//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)//to B plus water {int tmp=b-cur.y; next.x=cur.x; Next.y=b; Water from the faucet if (!mark[next.x][next.y]) {mark[next.x][next.y]=-tmp-n; Q.push (next); } if (cur.x>0)//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)//pour out 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%d%d", &a,&b,&c)!=-1) {memset (mark,0,sizeof (Mark)); flag=0; BFS (); if (!flag) printf ("impossible\n"); } return 0;}
Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.
POJ 3414 Pots (bfs+ Clues)