002:Pots

描述
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.

输入
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).

输出
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’.

样例输入

3 5 4

样例输出

6
FILL(2)
POUR(2,1)
DROP(1)
POUR(2,1)
FILL(2)
POUR(2,1)

就是倒水,每次都有6种选择,所以bfs就好了,与走迷宫类似,把当前两个水杯的状态记录就好了,所以直接上代码吧

#include<iostream>
#include<string>
#include<queue>
#include<algorithm>
using namespace std;
int a,b,c;
bool vis[150][150];
string oper[6] = {"FILL(1)\n","FILL(2)\n","POUR(1,2)\n","POUR(2,1)\n","DROP(1)\n","DROP(2)\n"}; //6种步骤,方便输出
struct Status {
    int pot_a,pot_b;//a,b两容器的体积
    string op; //操作
    Status(int a,int b,string _op,int _cnt) : pot_a(a),pot_b(b),op(_op),cnt(_cnt) {} //构造函数
    int cnt;//多少次
};
queue<Status> q;
int Bfs(int pota,int potb)
{
    vis[0][0] = 1;
    q.push(Status(0,0,"",0));
    while(!q.empty()) {
        Status status = q.front();
        if(status.pot_a == c || status.pot_b == c) { //找到答案,输出
            cout<< status.cnt << endl;
            cout << status.op;
            return 0;
        }
        for(int i = 0; i<6; ++i) { //相当于每个结点可以往6个方向拓展
            int now_a = status.pot_a,now_b = status.pot_b; //当前A,B容器的水量
            int tmp = now_a + now_b;
            switch(i) {
            case 0:
                now_a = a;
                break;
            case 1:
                now_b = b;
                break;
            case 2:
                now_b = min(tmp,b); //把A倒进B,最多肯定只能是b了
                now_a = max(tmp-b,0); //此时A中剩的肯定是多出来的,如果没有多就是0,不能是负数
                break;
            case 3:
                now_a = min(tmp,a);
                now_b = max(tmp-a,0);
                break;
            case 4:
                now_a = 0;
                break;
            case 5:
                now_b = 0;
                break;
            }
            if(!vis[now_a][now_b]) { //加入结点
                q.push(Status(now_a,now_b,status.op+oper[i],status.cnt+1));
                vis[now_a][now_b] = 1;
            }
        }
        q.pop();
    }
    return 1;
}
int main()
{
    ios::sync_with_stdio(false);
    cin>>a>>b>>c;
    if(Bfs(0,0)) //没有找到就输出impossible
        cout << "impossible" << endl;
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值