Pots POJ - 3414(BFS + 打印操作)

题目:

Problem
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)
Source
https://vjudge.net/problem/POJ-3414

思路:

这道题考察bfs求解及打印路径,结构体表示状态,操作仅几种,用数字表示并用vector存下
需要注意的是每种操作可执行的条件

这两道题还是非常的相似:https://blog.csdn.net/Jungle_st/article/details/104697157

代码:
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <string>
#include <vector>
#include <set>
#include <map>
#include<iomanip>
#include <stack>
#include <queue>
using namespace std;
typedef vector<int> vect;
const int INF = 0x3f3f3f3f;
string str[] = {"FILL(1)", "FILL(2)", "DROP(1)", "DROP(2)", "POUR(1,2)", "POUR(2,1)"};
/*                  0          1          2         3            4            5    */
struct P {
    int x, y;
    vect w;
    P(int x = 0, int y = 0, vect w = vect()): x(x), y(y), w(w){}
};

int a, b, k;
int d[110][110];

void final_print (const vect& w) {
    cout << w.size() << endl;
    for(int i = 0; i < w.size(); i++)
        cout << str[ w[i] ] << endl;
}
void bfs() {
    memset(d, INF, sizeof(d));
    d[0][0] = 0;

    queue<P> q;
    q.push(P(0, 0, vect()));

    while(!q.empty()) {

        P tmp = q.front(); q.pop();
        int x = tmp.x; int y = tmp.y;
        vect w = tmp.w;
        if(x == k || y == k) {
            final_print(w);
            return;
        }
                                                       // printf("%d %d %d\n", x, y, w.size());
        if(x < a) {//x加满 (x没满)
            vect tw = w;
            tw.push_back(0);
            if(tw.size() < d[a][y])
                q.push(P(a, y, tw)), d[a][y] = tw.size();
        }
        if(y < b) {//y加满(y没满)
            vect tw = w;
            tw.push_back(1);
            if(tw.size() < d[x][b])
                q.push(P(x, b, tw)), d[x][b] = tw.size();
        }

        if(x) {//x倒空(x不为空)
            vect tw = w;
            tw.push_back(2);
            if(tw.size() < d[0][y])
                q.push(P(0, y, tw)), d[0][y] = tw.size();
        }
        if(y) {//y倒空(y不为空)
            vect tw = w;
            tw.push_back(3);
            if(tw.size() < d[x][0])
                q.push(P(x, 0, tw)), d[x][0] = tw.size();
        }

        if(x && y<b && x+y<=b) {//x倒给y(x有,起初y没满,不溢出)
            vect tw = w;
            tw.push_back(4);
            if(tw.size() < d[0][x+y])
                q.push(P(0, x+y, tw)), d[0][x+y] = tw.size();
        }
        if(x && y<b && x+y>b) {//x倒给y(x有,起初y没满,x剩余)
            vect tw = w;
            tw.push_back(4);
            if(tw.size() < d[x+y-b][b])
                q.push(P(x+y-b, b, tw)), d[x+y-b][b] = tw.size();
        }

        if(y && x<a && x+y<=a) {//y倒给x(y有,起初x没满,不溢出)
            vect tw = w;
            tw.push_back(5);
            if(tw.size() < d[x+y][0])
                q.push(P(x+y, 0, tw)), d[x+y][0] = tw.size();
        }
        if(y && x<a && x+y>a) {//y倒给x(y有,起初x没满,y剩余)
            vect tw = w;
            tw.push_back(5);
            if(tw.size() < d[a][x+y-a])
                q.push(P(a, x+y-a, tw)), d[a][x+y-a] = tw.size();
        }
    }
    printf("impossible\n");
}

int main() {
    cin >> a >> b >> k;
    bfs();
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值