Pots --bfs

本文介绍了一种使用广度优先搜索(BFS)结合动态规划(DP)的方法来解决经典的水壶问题,即如何通过有限的操作步骤得到指定数量的水。通过具体的代码实现展示了如何在O(A*B)的时间复杂度内找到最优解,并提供了样例输入输出。

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

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)

解题报告

bfs的搜索永远优先最小操作数的,所以再用dp[][]记录防止重复即可在o(A*B)找到答案。

为了方便回溯路径,用一个back[][]记录前驱即可

//Accepted 760kB 0ms
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<queue>
#define MAX_N 102
using namespace std;
struct node{int last_A,last_B,op;};
int dp[MAX_N][MAX_N];
node back[MAX_N][MAX_N];
int A,B,goal;
char str[7][10]={"nothing","FILL(1)","FILL(2)","DROP(2)","DROP(1)","POUR(1,2)","POUR(2,1)"};

void print(int n_A,int n_B){
    node e=back[n_A][n_B];
    if(e.last_A||e.last_B)
        print(e.last_A,e.last_B);
    puts(str[e.op]);
}

void bfs(){
    queue<pair<int,int> >que;//first-> A  second-> B
    memset(dp,-1,sizeof(dp));
    que.push(make_pair(0,0));
    dp[0][0]=0;//dp record min steps from start

    while(!que.empty()){
        int n_A=que.front().first,n_B=que.front().second;que.pop();
        int s=dp[n_A][n_B];
        if(n_A==goal||n_B==goal){//back
            printf("%d\n",s);
            print(n_A,n_B);
            return ;
        }

        //操作一步有六种情况
        if(dp[A][n_B]==-1){//FILL(1)
            dp[A][n_B]=s+1;
            back[A][n_B]={n_A,n_B,1};
            que.push(make_pair(A,n_B));
        }
        if(dp[n_A][B]==-1){//FILL(2)
            dp[n_A][B]=s+1;
            back[n_A][B]={n_A,n_B,2};
            que.push(make_pair(n_A,B));
        }

        if(dp[n_A][0]==-1){//DROP(2)
            dp[n_A][0]=s+1;
            back[n_A][0]={n_A,n_B,3};
            que.push(make_pair(n_A,0));
        }
        if(dp[0][n_B]==-1){//DROP(1)
            dp[0][n_B]=s+1;
            back[0][n_B]={n_A,n_B,4};
            que.push(make_pair(0,n_B));
        }

        int tmpb=n_A+n_B>B?B:n_A+n_B;
        int tmpa=n_A+n_B-tmpb;
        if(dp[tmpa][tmpb]==-1){//POUR(1,2)
            dp[tmpa][tmpb]=s+1;
            back[tmpa][tmpb]={n_A,n_B,5};
            que.push(make_pair(tmpa,tmpb));
        }
        tmpa=n_A+n_B>A?A:n_A+n_B;
        tmpb=n_A+n_B-tmpa;
        if(dp[tmpa][tmpb]==-1){//POUR(2,1)
            dp[tmpa][tmpb]=s+1;
            back[tmpa][tmpb]={n_A,n_B,6};
            que.push(make_pair(tmpa,tmpb));
        }
    }
    puts("impossible");
}

int main()
{
    while(~scanf("%d%d%d",&A,&B,&goal)){
        bfs();
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值