POJ - 3414 https://cn.vjudge.net/problem/15208/origin
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升水,并且可以进行以下操作
FILL(i) 将第i个容器从水龙头里装满(1 ≤ i ≤ 2);
DROP(i) 将第i个容器抽干
POUR(i,j) 将第i个容器里的水倒入第j个容器(这次操作结束后产生两种结果,一是第j个容器倒满并且第i个容器依旧有剩余,二是第i个容器里的水全部倒入j中,第i个容器为空)
现在要求你写一个程序,来找出能使其中任何一个容器里的水恰好有C升,找出最少操作数并给出操作过程
Input
有且只有一行,包含3个数A,B,C(1<=A,B<=100,C<=max(A,B))
Output
第一行包含一个数表示最小操作数K
随后K行每行给出一次具体操作,如果有多种答案符合最小操作数,输出他们中的任意一种操作过程,如果你不能使两个容器中的任意一个满足恰好C升的话,输出“impossible”
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <queue>
#include <cstdlib>
using namespace std;
int a, b, c;
int vis[111][111];
void dfs(int x, int y);
char g[11][11] = {"FILL(1)", "FILL(2)", "DROP(1)", "DROP(2)", "POUR(1,2)", "POUR(2,1)"};
struct node
{
int a, b; //上一个状态两个杯子里的水。
int step, pre; pre用于记录由a,b状态变成现在i,j状态执行的操作。
} p[111][111];
struct xian
{
int a, b;
}xia; //记录当前状态两个杯子里的水
void bfs();
int main()
{
cin >> a >> b >> c;
memset(vis, 0, sizeof(vis));
memset(p, 0, sizeof(p));
bfs();
}
void dfs(int x, int y)
{
if(x == 0 && y == 0)
{
return ;
}
dfs(p[x][y].a, p[x][y].b); //地柜到最后输出第一步操作。
printf("%s\n",g[p[x][y].pre]);
}
void bfs()
{
queue<xian> q;
p[0][0] = (node)
{
-1, -1, 0, -1
};
q.push((xian){0, 0});
vis[0][0] = 1;
while(q.size())
{
xian now = q.front();
q.pop();
if(now.a == c || now.b == c)
{
cout << p[now.a][now.b].step <<endl;
dfs(now.a, now.b);
return;
}
for(int i = 0; i <= 5; i++)
{
xian nnow = now;
if(i == 0)
{
nnow.a = a;
}
else if(i == 1)
{
nnow.b = b;
}
else if(i == 2)
{
nnow.a = 0;
}
else if(i == 3)
{
nnow.b = 0;
}
else if(i == 4)
{
if(nnow.a + nnow.b <= b)
{
nnow.b = nnow.a + nnow.b; //还有下边的,nnow.b和nnow.a的变量谁先变的顺序一定不要反!!!!!
nnow.a = 0;
}
else
{
nnow.a = nnow.a+nnow.b-b;
nnow.b = b;
}
}
else if(i == 5)
{
if(nnow.a + nnow.b <= a)
{
nnow.a = nnow.a + nnow.b;
nnow.b = 0;
}
else
{
nnow.b = nnow.a+nnow.b-a;
nnow.a = a;
}
}
if(vis[nnow.a][nnow.b] == 0)
{
p[nnow.a][nnow.b] = (node){now.a, now.b, p[now.a][now.b].step + 1, i};
vis[nnow.a][nnow.b] = 1;
q.push(nnow);
}
}
}
cout << "impossible" <<endl;
}