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 jis 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的两个容器,初始时容器内水的体积都为0,有三种操作:
FILL(i):将 i 倒满水。
DROP(i):将 i 的水倒出。
POUR(i,j): 将 i 的水倒入 j 中,直至 i 为空,或 j 已满。
问当A,B中有一个容器装水量为C时进行的操作数,并打印需要的操作
题解:模拟杯子中水交互的六种状态,在Node中设置变量分别记录进行的操作类型,是否为头节点,再设一个Node数组记录路径
具体见代码
/*
bfs + 模拟 + 路径记录
*/
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<queue>
#include<stack>
#include<vector>
using namespace std;
const int MX = 105;
int A, B, C, flag[MX][MX]; //flag: 判断是否进行过该状态
string s;
struct Node{
int a, b;
int s, sign, par; //s: 操作数 sign: 判断进行的操作 par: 父节点存在与否
};
Node path[MX][MX]; //记录路径
void print(Node n)
{
vector<Node> v;
while(true)
{
v.push_back(n); //将路径压入vector中
if(n.par == 0) //不存在父节点
break;
n = path[n.a][n.b];
}
for(int i = v.size() - 1; i >= 0; i--)
{
if(v[i].sign == 1)
{
printf("POUR(1,2)\n");
}
else if(v[i].sign == 2)
{
printf("FILL(1)\n");
}
else if(v[i].sign == 3)
{
printf("DROP(1)\n");
}
else if(v[i].sign == 4)
{
printf("POUR(2,1)\n");
}
else if(v[i].sign == 5)
{
printf("FILL(2)\n");
}
else
{
printf("DROP(2)\n");
}
}
}
void bfs()
{
memset(flag, 0, sizeof(flag));
queue<Node> q;
Node now;
now.a = 0;
now.b = 0;
now.par = -1; //不存在父节点
now.s = 0;
now.sign = 0;
flag[0][0] = 1; //节点已使用过
q.push(now);
while(!q.empty())
{
now = q.front();
if(now.a == C || now.b == C) //任一杯子中的水等于C
{
printf("%d\n", now.s);
print(now);
return ;
}
Node node;
q.pop();
if(now.b != B && now.a) //POUR(now.a, now.b)
{
int x = B - now.b;
if(now.a >= x)
{
node.b = B;
node.a = now.a - x;
}
else
{
node.a = 0;
node.b = now.b + now.a;
}
node.s = now.s + 1; //操作数增加
node.sign = 1; //标记操作类型
if(!flag[node.a][node.b])
{
path[node.a][node.b] = now; //上一节点是now
flag[node.a][node.b] = 1; //标记已使用
node.par = now.par + 1; //父节点数增加(若为第一个节点,此时par已为0)
q.push(node);
}
}
if(now.a != A) //FILL(now.a)
{
node.a = A;
node.b = now.b;
node.s = now.s + 1;
node.sign = 2;
if(!flag[node.a][node.b])
{
path[node.a][node.b] = now;
flag[node.a][node.b] = 1;
node.par = now.par + 1;
q.push(node);
}
}
if(now.a) //DROP(now.a)
{
node.a = 0;
node.b = now.b;
node.s = now.s + 1;
node.sign = 3;
if(!flag[node.a][node.b])
{
path[node.a][node.b] = now;
flag[node.a][node.b] = 1;
node.par = now.par + 1;
q.push(node);
}
}
if(now.b && now.a != A) //POUR(now.b, now.a)
{
int x = A - now.a;
if(now.b >= x)
{
node.b = now.b - x;
node.a = A;
}
else
{
node.b = 0;
node.a = now.a + now.b;
}
node.s = now.s + 1;
node.sign = 4;
if(!flag[node.a][node.b])
{
path[node.a][node.b] = now;
flag[node.a][node.b] = 1;
node.par = now.par + 1;
q.push(node);
}
}
if(now.b != B) //FILL(now.b)
{
node.b = B;
node.a = now.a;
node.s = now.s + 1;
node.sign = 5;
if(!flag[node.a][node.b])
{
path[node.a][node.b] = now;
flag[node.a][node.b] = 1;
node.par = now.par + 1;
q.push(node);
}
}
if(now.b) //DROP(now.b)
{
node.b = 0;
node.a = now.a;
node.s = now.s + 1;
node.sign = 6;
if(!flag[node.a][node.b])
{
path[node.a][node.b] = now;
flag[node.a][node.b] = 1;
node.par = now.par + 1;
q.push(node);
}
}
}
printf("impossible\n");
}
int main()
{
while(scanf("%d%d%d", &A, &B, &C) != EOF)
{
bfs();
}
return 0;
}