简单的BFS加回溯(栈与队列的联合使用)



​​​​​​Pots

 POJ - 3414 

https://vjudge.net/problem/POJ-3414

You are given two pots, having the volume of A and B liters respectively. The following operations can be performed:

  1. FILL(i)        fill the pot i (1 ≤ ≤ 2) from the tap;
  2. DROP(i)      empty the pot i to the drain;
  3. 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 AB, 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’.

题目大意就是给三个整数A,B,C然后A,B分别是两个容器的容积,C是所给的要求,题目要求使用如下三种的操作:一,"FILL(2)"(用水龙头给B容器装满水)或者"FILL(1)"(用水龙头给A容器装满水);二,"DROP(2)"(把B容器的水全部倒掉)或者"DROP(1)"(把A 容器中的水全部倒掉);三,"FILL(1,2)"(将A 中的水倒给B容器,能倒满就倒满,过多了就剩在A容器中)或者"FILL(2,1)"(与前面同理);使用上述的三种操作使得A或者B容器中任意容器装C的水就行,C<=max(A,B);

废话不多解释,直接分情况开始广搜然后回溯,以下是AC的代码和详解:

#include<iostream>
#include<vector>
#include<cstring>
#include<algorithm>
#include<stack>
#include<queue>

using namespace std;

struct node {
    int x, y, step;
    string o;
    int fx, fy;
    // 构造函数
    node(int a, int b, int c, string d, int e, int f) {
        x = a;
        y = b;
        step = c;
        o = d;
        fx = e;
        fy = f;
    }
};

bool v[101][101]; // 访问数组,记录某状态是否已经访问过

int main() {
    int A, B, C;
    cin >> A >> B >> C;
    
    queue<node> q;  // BFS 队列
    stack<node> s;   // 用于记录路径
    q.push(node(0, 0, 0, "", 0, 0)); // 初始节点,两个水壶都为空
    v[0][0] = true;  // 标记初始状态为已访问
    int ans = 0;     // 存储答案

    while (!q.empty()) {
        node cur = q.front();
        q.pop();
        s.push(cur);  // 将当前节点加入路径栈

        // 如果已经达到了目标水量
        if (cur.x == C || cur.y == C) {
            ans = cur.step;  // 记录最小步数
            break;
        }

        // 操作水壶1的所有可能
        if (cur.x <= A) {
            int re = B - cur.y;  // 另一个水壶能接收的最大水量

            // 倒水操作:从水壶1倒到水壶2
            if (cur.x != 0 && re > cur.x && !v[0][cur.x + cur.y]) {
                q.push(node(0, cur.x + cur.y, cur.step + 1, "POUR(1,2)", cur.x, cur.y));
                v[0][cur.x + cur.y] = true;
            }
            if (cur.x != 0 && cur.x >= re && !v[cur.x - re][B]) {
                q.push(node(cur.x - re, B, cur.step + 1, "POUR(1,2)", cur.x, cur.y));
                v[cur.x - re][B] = true;
            }

            // FILL 操作:填满水壶1
            if (!v[A][cur.y]) {
                q.push(node(A, cur.y, cur.step + 1, "FILL(1)", cur.x, cur.y));
                v[A][cur.y] = true;
            }

            // DROP 操作:清空水壶1
            if (cur.x != 0 && !v[0][cur.y]) {
                q.push(node(0, cur.y, cur.step + 1, "DROP(1)", cur.x, cur.y));
                v[0][cur.y] = true;
            }
        }

        // 操作水壶2的所有可能
        if (cur.y <= B) {
            int re = A - cur.x;  // 水壶1能接收的最大水量

            // 倒水操作:从水壶2倒到水壶1
            if (cur.y != 0 && re > cur.y && !v[cur.x + cur.y][0]) {
                q.push(node(cur.x + cur.y, 0, cur.step + 1, "POUR(2,1)", cur.x, cur.y));
                v[cur.x + cur.y][0] = true;
            }
            if (cur.y != 0 && cur.y >= re && !v[A][cur.y - re]) {
                q.push(node(A, cur.y - re, cur.step + 1, "POUR(2,1)", cur.x, cur.y));
                v[A][cur.y - re] = true;
            }

            // FILL 操作:填满水壶2
            if (!v[cur.x][B]) {
                q.push(node(cur.x, B, cur.step + 1, "FILL(2)", cur.x, cur.y));
                v[cur.x][B] = true;
            }

            // DROP 操作:清空水壶2
            if (cur.y != 0 && !v[cur.x][0]) {
                q.push(node(cur.x, 0, cur.step + 1, "DROP(2)", cur.x, cur.y));
                v[cur.x][0] = true;
            }
        }
    }

    // 回溯路径,找到从终点到起点的操作序列
    stack<node> path;
    if (!s.empty()) {
        path.push(s.top());
        s.pop();
    }
    
    while (!s.empty()) {
        node cur = path.top();
        while (!s.empty() && !(cur.fx == s.top().x && cur.fy == s.top().y)) {
            s.pop();
        }
        if (!s.empty()) {
            path.push(s.top());
            s.pop();
        }
    }

    // 如果找到了答案,则输出路径
    if (ans) {
        cout << ans << endl;
        while (!path.empty()) {
            // 使用 c_str() 函数将 std::string 转换为 const char*
            cout << path.top().o.c_str() << endl;
            path.pop();
        }
    }
    else {
        cout << "impossible" << endl;
    }

    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值