POJ-3414-Pots

本文提供了一种使用广度优先搜索算法解决O-Pots问题的方法,目标是在给定两个容器体积A和B的情况下,通过一系列操作(如填充、倾倒、清空容器)达到容器之一装满C升水的目标。该算法详细解释了如何通过广度优先搜索策略最小化操作步骤,并提供了实现代码,包括输入输出示例。

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

O - Pots
Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
Submit

Status

Practice

POJ 3414
Description
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)

题意:有ab两个被子,给出ab的容量AB和目标水量C,操作时可以选择,充满a或b,a中的倒入b或反着倒,清空a或b,当有一个杯子的水量达到目标水量C时,输出最少步数,及操作过程。

难点可能就是在输出操作过程上,实际上是模拟加上广度优先搜索的思想

看代码吧,基本每行都写注释了

#include<stdio.h>
#include<string.h>
#include<string>
#include<stack>
#include<queue>
#include<math.h>
#include<limits.h>
#include<iostream>
#include<algorithm>
using namespace std;
//广度搜索
struct Node
{
    int a,b,step;
    char str[111][111];
};
int A,B,C;
bool mark[111][111];
bool bfs()
{
    memset(mark,false,sizeof(mark));
    queue<Node>que;
    Node p,q;
    p.a=0,p.b=0,p.step=0;
    que.push(p);
    mark[0][0]=true;
    while(!que.empty())
    {
        p=que.front();
        que.pop();
        if(p.a==C||p.b==C)
        {//找到终点水量
            printf("%d\n",p.step);
            for(int i=1; i<=p.step; i++)
            {//根据步数,将存储在str数组中的信息输出
                printf("%s\n",p.str[i]);
            }
            return true;//返回真,说明目标达到,结束函数
        }
        if(p.a==0)//如果a杯空着
        {
            q=p;//创建子节点
            q.a=A;//给它充满
            q.step++;//步数加一
            strcpy(q.str[q.step],"FILL(1)");//存入操作信息
            if(!mark[q.a][q.b])//如果a,b被子此时的状态没有出现过
            {
                mark[q.a][q.b]=true;//标记为已出现
                que.push(q);//子节点入队
            }
        }
        else if(p.a<=A)//如果a杯存量小于等于容量
        {
            q=p;//创建子节点
            q.a=0;//清空a杯
            q.step++;//步数加一
            strcpy(q.str[q.step],"DROP(1)");//根据步数记录操作信息
            if(!mark[q.a][q.b])//如果a,b被子此时的状态没有出现过
            {
                mark[q.a][q.b]=true;//标记为已出现
                que.push(q);//子节点入队
            }
            if(p.b<B)//如果b杯未满或为空
            {
                q=p;//创建子节点
                if(q.a+q.b<=B)//如果ab存量和小于等于b杯容量
                {
                    q.b+=q.a;//水都转移到b杯
                    q.a=0;//a杯清空
                }
                else//如果ab存量和大于b杯容量
                {
                    q.a=(q.b+q.a)-B;//ab存量大于b杯容量的部分存到a杯
                    q.b=B;//此时b杯充满
                }
                q.step++;//步数加一
                strcpy(q.str[q.step],"POUR(1,2)");//根据步数记录操作信息
                if(!mark[q.a][q.b])//如果此时ab的状态未出现过
                {
                    mark[q.a][q.b]=true;//记录为已出现
                    que.push(q);//结点入队
                }
            }
        }
        if(p.b==0)//b杯为空时
        {
            q=p;//创建子节点
            q.b=B;//b杯充满
            q.step++;//步数加一
            strcpy(q.str[q.step],"FILL(2)");//根据步数记录操作信息
            if(!mark[q.a][q.b])//如果此时ab的状态未曾出现过
            {
                mark[q.a][q.b]=true;//标记此时状态已出现
                que.push(q);//结点入队
            }
        }
        else if(p.b<=B)//如果b杯存量小于等于容量
        {
            q=p;//创建子节点
            q.b=0;//清空b杯
            q.step++;//步数加一
            strcpy(q.str[q.step],"DROP(2)");//根据步数记录操作信息
            if(!mark[q.a][q.b])//此时ab状态未曾出现过
            {
                mark[q.a][q.b]=true;//标记此状态已出现
                que.push(q);//结点入队
            }
            if(p.a<A)//如果a未满
            {
                q=p;//创建子节点
                if(q.b+q.a<=A)//如果ab存量和小于等于a容量
                {
                    q.a+=q.b;//ab存量转移到a杯
                    q.b=0;//b杯清空
                }
                else//如果ab存量大于a容量
                {
                    q.b=(q.b+q.a)-A;//多余的存量存到b杯
                    q.a=A;//a杯充满
                }
                q.step++;//步数加一
                strcpy(q.str[q.step],"POUR(2,1)");//根据步数记录操作信息
                if(!mark[q.a][q.b])//如果此时ab状态未曾出现过
                {
                    mark[q.a][q.b]=true;//标记为已出现
                    que.push(q);//结点入队
                }
            }
        }
    }
    return false;//如果都到这一步了,那肯定是无解了
}
int main()
{
    while(~scanf("%d%d%d",&A,&B,&C))
    {
        if(!bfs())puts("impossible");
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值