广搜+输出路径 POJ 3414 Pots

时间:2022-09-10 07:25:37

POJ 3414 Pots

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 13547   Accepted: 5718   Special Judge

Description

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

  1. FILL(i)        fill the pot i (1 ≤ ≤ 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 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 AB, 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)
 /*和之前的那道倒水题目十分相似,唯一的难点就是输出倒水的方式,我们可以记录每一个搜到的状态的前一状态在队列中的编号和,该状态与前一状态的关系,再递归输出结果即可*/
#include<iostream>
using namespace std;
#include<cstdio>
#include<cstring>
#define N 10010
int head=-,tail=-;
int a,b,c;
bool flag[][]={};
struct node{
int a1,b1,pre,dis,relat;
}que[N];
int bfs()
{
while(head<tail)
{
++head;
if(que[head].a1==c||que[head].b1==c)
{
return head;
}
if(!flag[que[head].a1][])
{
flag[que[head].a1][]=true;
node nex;
nex.a1=que[head].a1;nex.b1=;
nex.dis=que[head].dis+;
nex.pre=head;
nex.relat=;
++tail;
que[tail]=nex;
}
if(!flag[][que[head].b1])
{
flag[][que[head].b1]=true;
node nex;
nex.a1=;nex.b1=que[head].b1;
nex.dis=que[head].dis+;
nex.pre=head;
nex.relat=;
++tail;
que[tail]=nex;
}
if(!flag[que[head].a1][b])
{
flag[que[head].a1][b]=true;
node nex;
nex.a1=que[head].a1;nex.b1=b;
nex.dis=que[head].dis+;
nex.pre=head;
nex.relat=;
++tail;
que[tail]=nex;
}
if(!flag[a][que[head].b1])
{
flag[a][que[head].b1]=true;
node nex;
nex.a1=a;nex.b1=que[head].b1;
nex.dis=que[head].dis+;
nex.pre=head;
nex.relat=;
++tail;
que[tail]=nex;
}
if(que[head].a1>=(b-que[head].b1)&&!flag[que[head].a1-(b-que[head].b1)][b])
{
flag[que[head].a1-(b-que[head].b1)][b]=true;
node nex;
nex.a1=que[head].a1-(b-que[head].b1);nex.b1=b;
nex.dis=que[head].dis+;
nex.pre=head;
nex.relat=;
++tail;
que[tail]=nex;
}
if(que[head].a1<(b-que[head].b1)&&!flag[][que[head].a1+que[head].b1])
{
flag[][que[head].a1+que[head].b1]=true;
node nex;
nex.a1=;nex.b1=que[head].a1+que[head].b1;
nex.dis=que[head].dis+;
nex.pre=head;
nex.relat=;
++tail;
que[tail]=nex;
}
if(que[head].b1>=(a-que[head].a1)&&!flag[a][que[head].b1-(a-que[head].a1)])
{
flag[a][que[head].b1-(a-que[head].a1)]=true;
node nex;
nex.a1=a;nex.b1=que[head].b1-(a-que[head].a1);
nex.dis=que[head].dis+;
nex.pre=head;
nex.relat=;
++tail;
que[tail]=nex;
}
if(que[head].b1<(a-que[head].a1)&&!flag[que[head].a1+que[head].b1][])
{
flag[que[head].a1+que[head].b1][]=true;
node nex;
nex.a1=que[head].a1+que[head].b1;nex.b1=;
nex.dis=que[head].dis+;
nex.pre=head;
nex.relat=;
++tail;
que[tail]=nex;
}
}
return -;
}
void out(int temp)
{
if(que[que[temp].pre].pre!=-)
out(que[temp].pre);
if(que[temp].relat==)
printf("FILL(1)\n");
else if(que[temp].relat==)
printf("FILL(2)\n");
else if(que[temp].relat==)
printf("DROP(1)\n");
else if(que[temp].relat==)
printf("DROP(2)\n");
else if(que[temp].relat==)
printf("POUR(1,2)\n");
else printf("POUR(2,1)\n");
}
/*
FILL(2)
POUR(2,1)
DROP(1)
POUR(2,1)
FILL(2)
POUR(2,1)
*/
int main()
{
scanf("%d%d%d",&a,&b,&c);
++tail;
que[tail].a1=;que[tail].b1=;
que[tail].dis=;que[tail].pre=-;
flag[][]=true;
int temp=bfs();
if(temp==-)
printf("impossible\n");
else {
printf("%d\n",que[head].dis);
out(temp);
}
return ;
}