Pots
Description You are given two pots, having the volume of A and B liters respectively. The following operations can be performed:
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) Source
Northeastern Europe 2002, Western Subregion
|
/*
本题跟非常可乐非常类似,但是本题中有一个从水龙头接满水,
还有将水倒入下水道,这时候我选择了一个容量为200的瓶子来代替
水龙头和下水道的作用,大家可以想想看,相当于数学建模思想。
这时候有6种状态就是A倒B,B倒C等等。成为一个广搜问题,写一个
bfs就解决了,同时注意用fa数组存取父节点,这样就可以输出操作顺序了。
*/
#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
#include <cstdio>
#include <stack>
using namespace std;
const int maxn = 105;
int A, B, C, flag = 0;
int vis[maxn][maxn];
struct Node
{
int a,b,c,t;
}fa[maxn][maxn];
Node cur,pre;
void BFS()
{
memset(vis,0,sizeof(vis));
queue <Node> q;
pre.a = 0;
pre.b = 0;
pre.c = 205;
pre.t = 0;
vis[0][0] = 1;
q.push(pre);
while(!q.empty())
{
pre = q.front(); q.pop();
if(pre.a == C || pre.b == C)
{
flag = 1;
return ;
}
cur.a = A; cur.b = pre.b; cur.c = pre.c + pre.a - A; // c pour a
if(!vis[cur.a][cur.b] )
{
cur.t = pre.t + 1;
fa[cur.a][cur.b].a = pre.a;
fa[cur.a][cur.b].b = pre.b;
fa[cur.a][cur.b].c = 1;
vis[cur.a][cur.b] = 1;
q.push(cur);
}
cur.a = pre.a; cur.b = B; cur.c = pre.c + pre.b - B; // c pour b
if( !vis[cur.a][cur.b] )
{
cur.t = pre.t + 1;
fa[cur.a][cur.b].a = pre.a;
fa[cur.a][cur.b].b = pre.b;
fa[cur.a][cur.b].c = 2;
vis[cur.a][cur.b] = 1;
q.push(cur);
}
if(pre.a + pre.b >= B ) // a pour b
{
cur.a = pre.a + pre.b - B; cur.b = B; cur.c = pre.c;
if( !vis[cur.a][cur.b] )
{
cur.t = pre.t + 1;
fa[cur.a][cur.b].a = pre.a;
fa[cur.a][cur.b].b = pre.b;
fa[cur.a][cur.b].c = 3;
vis[cur.a][cur.b] = 1;
q.push(cur);
}
}
else
{
cur.b = pre.a + pre.b; cur.a = 0; cur.c = pre.c;
if( !vis[cur.a][cur.b] )
{
cur.t = pre.t + 1;
fa[cur.a][cur.b].a = pre.a;
fa[cur.a][cur.b].b = pre.b;
fa[cur.a][cur.b].c = 3;
vis[cur.a][cur.b] = 1;
q.push(cur);
}
}
cur.c = pre.c + pre.a; cur.a = 0; cur.b = pre.b; // a pour c
if( !vis[cur.a][cur.b] )
{
cur.t = pre.t + 1;
fa[cur.a][cur.b].a = pre.a;
fa[cur.a][cur.b].b = pre.b;
fa[cur.a][cur.b].c = 4;
vis[cur.a][cur.b] = 1;
q.push(cur);
}
if(pre.a + pre.b >= A ) // b pour a
{
cur.b = pre.a + pre.b - A; cur.a = A; cur.c = pre.c;
if( !vis[cur.a][cur.b] )
{
cur.t = pre.t + 1;
fa[cur.a][cur.b].a = pre.a;
fa[cur.a][cur.b].b = pre.b;
fa[cur.a][cur.b].c = 5;
vis[cur.a][cur.b] = 1;
q.push(cur);
}
}
else
{
cur.a = pre.a + pre.b; cur.b = 0; cur.c = pre.c;
if( !vis[cur.a][cur.b] )
{
cur.t = pre.t + 1;
fa[cur.a][cur.b].a = pre.a;
fa[cur.a][cur.b].b = pre.b;
fa[cur.a][cur.b].c = 5;
vis[cur.a][cur.b] = 1;
q.push(cur);
}
}
cur.c = pre.c + pre.b; cur.a = pre.a; cur.b = 0; // b pour c
if( !vis[cur.a][cur.b] )
{
cur.t = pre.t + 1;
fa[cur.a][cur.b].a = pre.a;
fa[cur.a][cur.b].b = pre.b;
fa[cur.a][cur.b].c = 6;
vis[cur.a][cur.b] = 1;
q.push(cur);
}
}
printf("impossible\n");
}
int main()
{
while(scanf("%d%d%d", &A, &B, &C) != EOF)
{
memset(fa,0,sizeof(fa));
flag = 0;
BFS();
if(flag)
{
stack <int> S;
int x = pre.a;
int y = pre.b;
int a,b;
while(fa[x][y].c)
{
S.push(fa[x][y].c);
a = x;
b = y;
x = fa[a][b].a;
y = fa[a][b].b;
}
printf("%d\n", pre.t);
while(!S.empty())
{
int temp = S.top();
S.pop();
if(temp == 1)
printf("FILL(1)\n");
else if(temp == 2)
printf("FILL(2)\n");
else if(temp == 3)
printf("POUR(1,2)\n");
else if(temp == 4)
printf("DROP(1)\n");
else if(temp == 5)
printf("POUR(2,1)\n");
else
printf("DROP(2)\n");
}
}
}
return 0;
}