Time Limit: 1000MS | Memory Limit: 65536K | |||
Total Submissions: 13419 | Accepted: 5653 | 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)
两个瓶子,接水,倒水,看能否达到目标。
bfs,六种操作。
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <string>
using namespace std;
int C, Sum[5];
int vis[110][110];
struct Pos //保存当前状态
{
int p[5], steps; //p[1],p[2] 两个水壶的水,steps 操作次数
string road; //记录如何操作
} Cur, Next;
queue<Pos> Q;
string toString(char ch, int num) //将操作转换为字符串
{
string str = "";
str += ch, str += num + '0';
return str;
}
void Push() //判断当前状态是否已经进过队列
{
if (!vis[Next.p[1]][Next.p[2]])
Q.push(Next);
vis[Next.p[1]][Next.p[2]] = 1;
}
void Fill(int x) //从水龙头将p[x]装满
{
Next.p[x] = Sum[x];
Next.p[3 - x] = Cur.p[3 - x]; //另一个不变
Next.road = Cur.road + toString('F', x); //记录操作
Push();
}
void Drop(int x) //p[x]倒空,倒到下水道
{
Next.p[x] = 0;
Next.p[3 - x] = Cur.p[3 - x];
Next.road = Cur.road + toString('D', x);
Push();
}
void Pour(int x, int y)
{
if (Sum[y] - Cur.p[y] >= Cur.p[x]) //p[x]向p[y]倒,将p[x]倒空
{
Next.p[x] = 0;
Next.p[y] = Cur.p[y] + Cur.p[x];
}
else //将p[y]倒满
{
Next.p[y] = Sum[y];
Next.p[x] = Cur.p[x] - (Sum[y] - Cur.p[y]);
}
Next.road = Cur.road + toString('P', x);
Push();
}
void bfs()
{
while (!Q.empty()) Q.pop();
memset(vis, 0, sizeof(vis)); //标记状态
Cur.p[1] = Cur.p[2] = 0; //初始状态
Cur.steps = 0;
Cur.road = "";
vis[0][0] = 1;
Q.push(Cur);
while (!Q.empty())
{
Cur = Q.front();
Q.pop();
if (Cur.p[1] == C || Cur.p[2] == C) //达到目标
{
printf("%d\n", Cur.steps); //操作次数
int len = Cur.road.length();
for (int i = 0; i < len; i++) //打印操作方法
{
if (Cur.road[i] == 'F')
{
printf("FILL(%d)\n", Cur.road[i + 1] == '1' ? 1 : 2);
}
else if (Cur.road[i] == 'P')
{
if (Cur.road[i + 1] == '1')
printf("POUR(1,2)\n");
else
printf("POUR(2,1)\n");
}
else if (Cur.road[i] == 'D')
{
printf("DROP(%d)\n", Cur.road[i + 1] == '1' ? 1 : 2);
}
}
return;
}
Next.steps = Cur.steps + 1; //次数加1
if (Cur.p[1] < Sum[1])
{
Fill(1); //从水龙头装满p[1]
}
if (Cur.p[2] < Sum[2])
{
Fill(2); //从水龙头装满p[2]
}
if (Cur.p[1] != 0) //p[1]向外倒
{
Drop(1);
Pour(1, 2);
}
if (Cur.p[2] != 0) //p[2]向外倒
{
Drop(2);
Pour(2, 1);
}
}
printf("impossible\n"); //无法完成
}
int main()
{
while (~scanf("%d%d%d", &Sum[1], &Sum[2], &C))
{
bfs();
}
return 0;
}