Pots
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。
- FILL(i):将i号杯子倒满
- DORP(i):将i号杯子的水全部倒掉
- POUR(i,j):将i号杯子的水倒入j号杯
题解: 利用dfs分情况入队:
- 倒满1号杯,将此时两个杯子中盛水的状态入队
- 倒满2号杯,入队
- 倒空1号杯,入队
- 倒空2号杯,入队
- 将1号杯倒入2号杯,入队
- 将2号杯倒入1号杯,入队
详细逻辑见代码:
c++ AC 代码
#include<cstdio>
#include<queue>
#include<cstring>
#include<cstdlib>
#define size 200
using namespace std;
int vis[size][size]; // 代表1号杯和2号杯的状态一维坐标代表1号杯状态,二维代表2号杯状态
int A, B, C;
struct node
{
int v1; // 1号杯子中的水的体积
int v2; // 2号杯子中的水的体积
int pre_v1; // 上个状态1号杯子的水的体积
int pre_v2; // 上个状态2号杯子的水的体积
int way; // 操作1~6
int step; // 当前步数
}dp_ab[size][size];
queue<node> que;
void bfs();
void print(int ,int);
int main()
{
memset(vis,0,sizeof(vis)); // 初始化所有状态
dp_ab[0][0].v1 = dp_ab[0][0].v2 = 0; // 刚开始两个杯子都是空的
dp_ab[0][0].pre_v1 = dp_ab[0][0].pre_v2 = -1; // 他们前面没有状态,他们本身就是初始状态
dp_ab[0][0].step = 0; // 还没有进行操作,初始化为0
scanf("%d%d%d",&A,&B,&C);
bfs();
// system("pause");
return 0;
}
void print(int a,int b)
{
if(dp_ab[a][b].v1 == 0 && dp_ab[a][b].v2 == 0) // 达到初始状态
return;
print(dp_ab[a][b].pre_v1,dp_ab[a][b].pre_v2);
switch (dp_ab[a][b].way)
{
case 1: puts("FILL(1)");break;
case 2: puts("FILL(2)");break;
case 3:puts("DROP(1)");break;
case 4:puts("DROP(2)");break;
case 5:puts("POUR(1,2)");break;
case 6:puts("POUR(2,1)");break;
default:
break;
}
}
void push(node d)
{
if(!vis[d.v1][d.v2]) // 如果之前没有存在过这个状态,把这个状态的1号杯和2号杯进队
{
que.push(d);
vis[d.v1][d.v2] = 1; // 标记这个状态已经存在
dp_ab[d.v1][d.v2] = d; // 将该节点存放到1、2号杯体积相对应的数组节点中
}
}
void bfs()
{
que.push(dp_ab[0][0]);
while(!que.empty())
{
node pre_node = que.front();
que.pop();
if(pre_node.v1 == C || pre_node.v2 == C)
{
printf("%d\n",pre_node.step);
print(pre_node.v1,pre_node.v2);
return;
}
node now;
now.pre_v1 = pre_node.v1;
now.pre_v2 = pre_node.v2;
now.step = pre_node.step + 1;
if(pre_node.v1 != A) // 把1加满
{
now.v1 = A;
now.v2 = pre_node.v2; // 加满1,但是2的状态不变,所以还是继承原来的状态
now.way = 1; // 操作为1号操作
push(now);
}
if(pre_node.v2 != B) // 把2加满
{
now.v2 = B;
now.v1 = pre_node.v1;
now.way = 2;
push(now);
}
if(pre_node.v1 != 0) // 把1号杯倒掉
{
now.v1 = 0;
now.v2 = pre_node.v2;
now.way = 3;
push(now);
}
if(pre_node.v2 != 0) // 把2倒光
{
now.v2 = 0;
now.v1 = pre_node.v1;
now.way = 4;
push(now);
}
if(pre_node.v1 && pre_node.v2 != B) // 把1倒给2
{
if(pre_node.v1+pre_node.v2 >= B)
{
now.v2 = B;
now.v1 = pre_node.v1 + pre_node.v2 - B;
}
else
{
now.v2 = pre_node.v1 + pre_node.v2;
now.v1 = 0;
}
now.way = 5;
push(now);
}
if(pre_node.v1 != A && pre_node.v2) // 把2倒给1
{
if(pre_node.v1 + pre_node.v2 >= A)
{
now.v1 = A;
now.v2 = pre_node.v1 + pre_node.v2 - A;
}
else
{
now.v1 = pre_node.v1 + pre_node.v2;
now.v2 = 0;
}
now.way = 6;
push(now);
}
}
puts("impossible");
}
代码参考自:https://blog.csdn.net/qq_43301061/article/details/85261352