Description
You are given two pots, having 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 is full (and there may
- be some water left in the pot i), or the pot i is empty (and all its contents have been moved to the pot j).
Write a program to find the shortest possible sequence of these operations that will yield exactly
C liters of water in one of the pots.
Input
On the first and only line are the numbers A, B, and C. These are all integers in the range from
1 to 100 and 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 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)
题意:给出两个容积分别为 a 和 b 的pot,按照以下三种操作方式,
求出能否在一定步数后,使者两个pot的其中一个的水量为c。
1.FILL(i):将ipot倒满水。
2.DROP(i):将ipot倒空水。
3.POUR(i,j): 将ipot的水倒到jpot上,直至要么ipot为空,要么jpot为满。
我分了6种情况
#include
#include
#include
#include
using namespace std;
int a[20105],b[1110005];
int s,n,m,x,y;
struct node
{
int nn,mm,tep;
};
node map[102][102];
node xx,yy;
queue
Q; int bfs() { while(!Q.empty())Q.pop(); xx.nn=xx.mm=xx.tep=0; Q.push(xx); while(!Q.empty()) { xx=Q.front(); yy.tep=xx.tep+1; Q.pop(); if(xx.mm>0) { x=yy.mm=0; ///m=0; y=yy.nn=xx.nn; if(a[yy.mm*111+yy.nn]==0) { map[yy.mm][yy.nn].tep=22; map[yy.mm][yy.nn].mm=xx.mm; map[yy.mm][yy.nn].nn=xx.nn; if(yy.mm==s||yy.nn==s) return yy.tep; a[yy.mm*111+yy.nn]=1; Q.push(yy); } y=yy.nn=min(n,xx.mm+xx.nn); ///m->n x=yy.mm=xx.mm+xx.nn-yy.nn; if(a[yy.mm*111+yy.nn]==0) { map[yy.mm][yy.nn].tep=6; map[yy.mm][yy.nn].mm=xx.mm; map[yy.mm][yy.nn].nn=xx.nn; if(yy.mm==s||yy.nn==s) return yy.tep; a[yy.mm*111+yy.nn]=1; Q.push(yy); } } if(xx.mm
0) { y=yy.nn=0; ///n=0; x=yy.mm=xx.mm; if(a[yy.mm*111+yy.nn]==0) { map[yy.mm][yy.nn].tep=11; map[yy.mm][yy.nn].mm=xx.mm; map[yy.mm][yy.nn].nn=xx.nn; if(yy.mm==s||yy.nn==s) return yy.tep; a[yy.mm*111+yy.nn]=1; Q.push(yy); } x=yy.mm=min(m,xx.mm+xx.nn); ///n->m y=yy.nn=xx.mm+xx.nn-yy.mm; if(a[yy.mm*111+yy.nn]==0) { map[yy.mm][yy.nn].tep=5; map[yy.mm][yy.nn].mm=xx.mm; map[yy.mm][yy.nn].nn=xx.nn; if(yy.mm==s||yy.nn==s) return yy.tep; a[yy.mm*111+yy.nn]=1; Q.push(yy); } } if(xx.nn
=0; i--) { if(b[i]==5) printf("POUR(1,2)\n"); if(b[i]==6) printf("POUR(2,1)\n"); if(b[i]==22) printf("DROP(2)\n"); if(b[i]==11) printf("DROP(1)\n"); if(b[i]==1) printf("FILL(1)\n"); if(b[i]==2) printf("FILL(2)\n"); } } } return 0; }