Pots
Time Limit: 1000MS | Memory Limit: 65536K | |||
Total Submissions: 36585 | Accepted: 14868 | Special Judge |
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,还有三种操作:倒满水,倒掉水,互相倒水,问什么时候能倒出C多的水?
思路
两个杯子A和B,还有三种操作,对应到A和B上就是六种操作:
1 把a倒满
2 把b倒满
3 把a倒掉
4 把b全倒掉
5 把a倒入b
6 把b倒入a
题目让求最小步数和操作步骤,很容易想到BFS+回溯
因此搜索的时候,就按照这六种方案进行操作,直到有一个杯子中的水是C,就解决了
代码如下:
其中回溯相对BFS要麻烦一点,重新申请一个结构体数组把A和B的水对于的上一步的水和操作保存一下就可以了……
/*
1 把a倒满 2 把b倒满 3 把a倒掉 4 把b全倒掉 5 把a倒入b 6 把b倒入a
*/
#include <iostream>
#include <queue>
using namespace std;
struct node
{
int a;
int b;
int step;
int flag;
int last_a, last_b;
};
node last[105][105];
int vis[105][105];
queue<node> q;
int a, b, c;
void P(int sa, int sb)
{
if (sa == 0 && sb == 0) //终止条件
return;
P(last[sa][sb].last_a, last[sa][sb].last_b); //回溯 把以前的路径打印出来
switch (last[sa][sb].flag)
{
case 1:
cout << "FILL(1)" << endl;
break;
case 2:
cout << "FILL(2)" << endl;
break;
case 3:
cout << "DROP(2)" << endl;
break;
case 4:
cout << "DROP(1)" << endl;
break;
case 5:
cout << "POUR(2,1)" << endl;
break;
case 6:
cout << "POUR(1,2)" << endl;
break;
default:
break;
}
}
void bfs()
{
node start;
start.a = 0, start.b = 0, start.flag = 0, start.step = 0, start.last_a = 0, start.last_b = 0;
q.push(start);
vis[0][0] = 1;
while (q.size())
{
node data;
data = q.front();
q.pop();
int tema = data.a, temb = data.b, temstep = data.step, temflag = data.flag;
data.last_a = data.a, data.last_b = data.b;
if (data.a == c || data.b == c)
{
cout << data.step << endl;
P(data.a, data.b);
return;
}
if (data.a < a && !vis[a][data.b]) //把a倒满
{
data.a = a;
data.step++;
data.flag = 1;
vis[data.a][data.b] = 1;
//回溯准备
last[data.a][data.b].flag = data.flag;
last[data.a][data.b].last_a = tema;
last[data.a][data.b].last_b = temb;
q.push(data);
//还原一下 不影响后面的if
data.a = tema, data.b = temb, data.step = temstep, data.flag = temflag;
}
if (data.b < b && !vis[data.a][b]) //把b倒满
{
data.b = b;
data.step++;
data.flag = 2;
vis[data.a][data.b] = 1;
last[data.a][data.b].flag = data.flag;
last[data.a][data.b].last_a = tema;
last[data.a][data.b].last_b = temb;
q.push(data);
data.a = tema, data.b = temb, data.step = temstep, data.flag = temflag;
}
if (data.b != 0 && !vis[data.a][0]) //把b倒了
{
data.b = 0;
data.step++;
data.flag = 3;
vis[data.a][data.b] = 1;
last[data.a][data.b].flag = data.flag;
last[data.a][data.b].last_a = tema;
last[data.a][data.b].last_b = temb;
q.push(data);
data.a = tema, data.b = temb, data.step = temstep, data.flag = temflag;
}
if (data.a != 0 && !vis[0][data.b]) //把a倒了
{
data.a = 0;
data.step++;
data.flag = 4;
vis[data.a][data.b] = 1;
last[data.a][data.b].flag = data.flag;
last[data.a][data.b].last_a = tema;
last[data.a][data.b].last_b = temb;
q.push(data);
data.a = tema, data.b = temb, data.step = temstep, data.flag = temflag;
}
if (data.a != a && data.b != 0) // b->a
{
int t = data.a;
data.step++;
data.flag = 5;
data.a += data.b;
if (data.a > a)
{
data.a = a;
data.b = data.b - (a - t);
}
else
{
data.b = 0;
}
if (!vis[data.a][data.b])
{
vis[data.a][data.b] = 1;
last[data.a][data.b].flag = data.flag;
last[data.a][data.b].last_a = tema;
last[data.a][data.b].last_b = temb;
q.push(data);
}
data.a = tema, data.b = temb, data.step = temstep, data.flag = temflag;
}
if (data.a != 0 && data.b != b) // a->b
{
int t = data.b;
data.step++;
data.flag = 6;
data.b += data.a;
if (data.b > b)
{
data.b = b;
data.a = data.a - (b - t);
}
else
{
data.a = 0;
}
if (!vis[data.a][data.b])
{
vis[data.a][data.b] = 1;
last[data.a][data.b].flag = data.flag;
last[data.a][data.b].last_a = tema;
last[data.a][data.b].last_b = temb;
q.push(data);
}
data.a = tema, data.b = temb, data.step = temstep, data.flag = temflag;
}
}
cout << "impossible";
}
int main()
{
cin >> a >> b >> c;
bfs();
return 0;
}