POJ-3414 Pots
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)
my analysis
This problem is implicit graph, using BFS to solve, and finally using stack output operation
solution(C++)
#include<iostream>
#include<queue>
#include<stack>
#include<string>
using namespace std;
int A, B, a = 0, b = 0, C;
bool inq[10000][10000] = { false };
struct bottle
{
int a;
int b;
int pre;
int now;
string ope;
}bot;
struct bottle path[10000];
int sum = 0;
int BFS()
{
queue<bottle> q;
bot.a = 0, bot.b = 0;
bot.pre = -1, bot.now = 0;
path[0] = bot;
q.push(bot);
while (!q.empty())
{
bottle top = q.front();
q.pop();
if (top.a == C || top.b == C)
return top.now;
//six operations
if (top.a < A&&inq[A][top.b] == false)
{
inq[A][top.b] = true;
bot.a = A, bot.b = top.b;
bot.pre = top.now, bot.now = ++sum;
bot.ope = "FILL(1)";
path[sum] = bot;
q.push(bot);
}
if (top.b < B&&inq[top.a][B] == false)
{
inq[top.a][B] = true;
bot.a = top.a, bot.b = B;
bot.pre = top.now, bot.now = ++sum;
bot.ope = "FILL(2)";
path[sum] = bot;
q.push(bot);
}
if (top.a != 0 && inq[0][top.b] == false)
{
inq[0][top.b] = true;
bot.a = 0, bot.b = top.b;
bot.pre = top.now, bot.now = ++sum;
bot.ope = "DROP(1)";
path[sum] = bot;
q.push(bot);
}
if (top.b != 0 && inq[top.a][0] == false)
{
inq[top.a][0] = true;
bot.a = top.a, bot.b = 0;
bot.pre = top.now, bot.now = ++sum;
bot.ope = "DROP(2)";
path[sum] = bot;
q.push(bot);
}
if (top.b + top.a >= B && inq[top.a - (B - top.b)][B] == false)//pour A to B
{
inq[top.a - (B - top.b)][B] = true;
bot.a = top.a - (B - top.b), bot.b = B;
bot.pre = top.now, bot.now = ++sum;
bot.ope = "POUR(1,2)";
path[sum] = bot;
q.push(bot);
}
if (top.b + top.a < B && inq[0][top.b + top.a] == false)
{
inq[0][top.b + top.a] = true;
bot.a = 0, bot.b = top.b + top.a;
bot.pre = top.now, bot.now = ++sum;
bot.ope = "POUR(1,2)";
path[sum] = bot;
q.push(bot);
}
if (top.b + top.a >= A && inq[A][top.b-(A-top.a)] == false)//pour B to A
{
inq[A][top.b - (A - top.a)] = true;
bot.a = A, bot.b = top.b - (A - top.a);
bot.pre = top.now, bot.now = ++sum;
bot.ope = "POUR(2,1)";
path[sum] = bot;
q.push(bot);
}
if (top.b + top.a < A && inq[top.b + top.a][0] == false)
{
inq[top.b + top.a][0] = true;
bot.a = top.b + top.a, bot.b = 0;
bot.pre = top.now, bot.now = ++sum;
bot.ope = "POUR(2,1)";
path[sum] = bot;
q.push(bot);
}
}
return -1;
}
int main()
{
cin >> A >> B >> C;
stack<bottle> p;
int res = BFS();
if (res == -1)
cout << "impossible" << endl;
else
{
while (res != 0)
{
p.push(path[res]);
res = path[res].pre;
}
cout << p.size() << endl;
while (!p.empty())
{
cout << p.top().ope << endl;
p.pop();
}
}
}