H - Pots POJ - 3414

本文介绍了一种解决特定水罐问题的算法实现,通过BFS(宽度优先搜索)算法来寻找从两个不同容量的水罐中得到目标水量的最短操作步骤。该算法能够输出达到目标水量所需的精确操作序列,适用于容量范围为1到100升的水罐,并能处理无法达成目标的情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 ≤ i ≤ 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 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)
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <cstring>

using namespace std;
typedef long long ll;
typedef struct node {
    int i, j;
    string path;
} node;
const int maxn = 1e3 + 10;
int t, n, m, cnt, cnt2;
int vis[maxn][maxn];
char s[maxn], s2[maxn], s3[maxn], str[maxn];
bool BFS() {
    memset(vis, 0, sizeof(vis));
    queue<node> Q;
    node p;
    p.i = n;
    p.j = 0;
    p.path = '1';
    Q.push(p);
    p.i = 0;
    p.j = m;
    p.path = '2';
    Q.push(p);
    vis[0][m] = vis[n][0] = 1;
    while(!Q.empty()) {
        p = Q.front();
        Q.pop();
        if(p.i == t || p.j == t) {
            int len = p.path.size();
            cout << len << endl;
            for(int i = 0; i < len; i++) {
                if(p.path[i] == '1') {
                    cout << "FILL(1)\n";
                } else if(p.path[i] == '2') {
                    cout << "FILL(2)\n";
                } else if(p.path[i] == '3') {
                    cout << "DROP(1)\n";
                } else if(p.path[i] == '4') {
                    cout << "DROP(2)\n";
                } else if(p.path[i] == '5') {
                    cout << "POUR(1,2)\n";
                } else {
                    cout << "POUR(2,1)\n";
                }
            }
            return 1;
        } else {
            node nn;
            nn.i = n;
            nn.j = p.j;
            nn.path = p.path + '1';
            if(!vis[nn.i][nn.j]) {
                Q.push(nn);
                vis[nn.i][nn.j] = 1;
            }
            nn.i = p.i;
            nn.j = m;
            nn.path = p.path + '2';
            if(!vis[nn.i][nn.j]) {
                Q.push(nn);
                vis[nn.i][nn.j] = 1;
            }
            nn.i = 0;
            nn.j = p.j;
            nn.path = p.path + '3';
            if(!vis[nn.i][nn.j]) {
                Q.push(nn);
                vis[nn.i][nn.j] = 1;

            }
            nn.i = p.i;
            nn.j = 0;
            nn.path = p.path + '4';
            if(!vis[nn.i][nn.j]) {
                Q.push(nn);
                vis[nn.i][nn.j] = 1;

            }
            if(p.i + p.j > m) {
                nn.j = m;
                nn.i = p.i + p.j - m;
            } else {
                nn.i = 0;
                nn.j = p.i + p.j;
            }
            nn.path = p.path + '5';
            if(!vis[nn.i][nn.j]) {
                Q.push(nn);
                vis[nn.i][nn.j] = 1;

            }
            if(p.i + p.j > n) {
                nn.i = n;
                nn.j = p.i + p.j - n;
            } else {
                nn.i = p.i + p.j;
                nn.j = 0;
            }
            nn.path = p.path + '6';
            if(!vis[nn.i][nn.j]) {
                Q.push(nn);
                vis[nn.i][nn.j] = 1;

            }
        }
    }
    return 0;
}
int main() {
    ios::sync_with_stdio(false);
    cin >> n >> m >> t;
    int ans = BFS();
    if(!ans) {
        cout << "impossible\n";
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值