POJ 3414 Pots (经典bfs )

【代码超详解】POJ 3414 Pots(BFS,0 ms) 一、题目描述 Pots Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 29772 Accepted: 12428 Special Judge Description You are given two pots, having the volume of A and B liters respectively. The ... 阅读详情

Description

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)

Source

Northeastern Europe 2002, Western Subregion

两个壶,可以清空,可以加满,可以相互倒水,不过每次到要么这个壶水倒完,要么那个壶满了,要求一个壶有一定的水结束,输出步骤

bfs具体在代码中


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
#define N 105

struct stud{
int a,b;
int time;
string mv;
};

int a,b,c;

int vis[N][N];

int judge(int x,int y)
{
    if(x==c||y==c)
        return 1;
    return 0;
}

stud bfs()
{
    struct stud next,cur;
    queue<stud>q;

    while(!q.empty())
        q.pop();

    cur.a=cur.b=0;
    cur.mv="";
    cur.time=0;

    if(cur.a==c||cur.b==c)
        return cur;

    q.push(cur);

    memset(vis,0,sizeof(vis));
    vis[0][0]=1;

    while(!q.empty())
    {
        cur=q.front();
        q.pop();

        if(cur.a<a&&!vis[a][cur.b]) //装满a
        {
            next=cur;
            next.a=a;
            next.time++;
            next.mv+="F1";
            vis[next.a][next.b];

            if(judge(next.a,next.b)) return next;

            q.push(next);
        }

        if(cur.b<b&&!vis[cur.a][b])   //装满b
        {

            next=cur;
            next.b=b;
            next.time++;
            next.mv+="F2";
            vis[next.a][next.b]=1;

            if(judge(next.a,next.b)) return next;
            q.push(next);
        }

        if(cur.a>0&&!vis[0][cur.b])   //倒出a
        {
            next.a=0;
            next.b=cur.b;
            next.mv=cur.mv;
            next.mv+="D1";
            next.time=cur.time+1;

            vis[next.a][next.b]=1;


            if(judge(next.a,next.b)) return next;
            q.push(next);
        }

        if(cur.b>0&&!vis[cur.a][0])    //倒出b
        {
            next.b=0;
            next.a=cur.a;
            next.mv=cur.mv;
            next.mv+="D2";
            next.time=cur.time+1;

            vis[next.a][next.b]=1;


            if(judge(next.a,next.b)) return next;
            q.push(next);
        }

        //a给b倒水

        if(cur.b<b&&cur.a>0&&cur.a+cur.b<=b&&!vis[0][cur.a+cur.b])
        {
            next.a=0;
            next.b=cur.a+cur.b;
            next.time=cur.time+1;
            next.mv=cur.mv+"P12";

            vis[next.a][next.b]=1;

             if(judge(next.a,next.b)) return next;
            q.push(next);
        }
        else
          if(cur.b<b&&cur.a>0&&cur.a+cur.b>b&&!vis[cur.a+cur.b-b][b])
          {
              next.a=cur.a+cur.b-b;
              next.b=b;
              next.time=cur.time+1;
              next.mv=cur.mv+"P12";

              vis[next.a][next.b]=1;

              if(judge(next.a,next.b)) return next;
              q.push(next);
          }

          //b给a倒水

          if(cur.b>0&&cur.a!=a&&cur.a+cur.b<=a&&!vis[cur.a+cur.b][0])
          {
              next.a=cur.a+cur.b;
              next.b=0;
              next.time=cur.time+1;
              next.mv=cur.mv+"P21";

              vis[next.a][next.b]=1;
               if(judge(next.a,next.b)) return next;
              q.push(next);
          }
          else
            if(cur.b>0&&cur.a!=a&&cur.a+cur.b>a&&!vis[a][cur.a+cur.b-a])
          {
              next.a=a;
              next.b=cur.a+cur.b-a;
              next.time=cur.time+1;
              next.mv=cur.mv+"P21";

              vis[next.a][next.b]=1;
               if(judge(next.a,next.b)) return next;
              q.push(next);

          }

    }

   struct stud ans;
    ans.a=-1;
    return ans;
}

int main()
{
        int i;
        scanf("%d%d%d",&a,&b,&c);
        struct stud cur;

        cur=bfs();

        if(cur.a==-1)
        {
            printf("impossible\n");
            return 0;
        }

        printf("%d\n",cur.time);

        int len=cur.mv.size();

        for(i=0;i<len;i++)
        {
            if(cur.mv[i]=='D')
            {
                printf("DROP(");
                i++;
                printf("%c)\n",cur.mv.at(i));
            }
            else
            if(cur.mv[i]=='F')
            {
                printf("FILL(");
                i++;
                printf("%c)\n",cur.mv.at(i));
            }
            else
            {
                printf("POUR(");
                i++;
                printf("%c",cur.mv.at(i));
                i++;
                printf(",%c)\n",cur.mv.at(i));
            }
        }
    return 0;
}



微店构造订单页面,自动化抢票 之前的文章写过微店抢票如何构造订单页面分析,这篇文章在之前的基础上,进行抢票。 阅读详情

相关推荐

别再只会regress了!用Stata的csdid命令搞定多期DID的完整实操指南

本文详细介绍了如何在Stata中使用csdid命令进行多期DID分析,解决传统DID方法在交错处理场景下的局限性。通过完整的实操指南,包括环境搭建、核心语法、结果解读和高阶应用,帮助研究者更准确地评估政策效果,适用于计量经济学和实证研究领域。

weixin_30236595的博客 582

poj 3414 pots [经典 BFS ]

#include #include #include #include #include #include #include #define Bug cout << "here\n"; using namespace std; const int N = 101; struct node { int k1, k2, steps; int op; node *

so true_interview / 457

AutoDock Vina多配体对接 (Simultaneous Multiple Ligand Docking)

多配体分子对接(Simultaneous Multiple Ligand Docking, SMLD)或(Multiple Ligand Simultaneous Docking, MLSD)是一种分子对接技术,用于将多个配体(小分子药物候选物)同时对接到一个蛋白质分子的结合位点。与传统的分子对接技术不同,传统方法通常只考虑一个配体与一个蛋白质的相互作用。SMLD的目标是更高效地研究多个配体如何与同一个蛋白质相互作用,以寻找潜在的药物候选物。

MurphyStar的博客 7143

倒水问题

HDU1495 非常可乐【倒水问题+BFS】 POJ3414 Pots【倒水问题+BFS】

兜率工的博客 656

LabVIEW小白福音:手把手教你用NI采集卡搭建振动信号采集系统(附源码和避坑指南)

本文为LabVIEW初学者提供详细的NI采集卡振动信号采集系统搭建指南,涵盖硬件连接、驱动安装、信号处理及性能优化等关键步骤。特别针对振动信号采集系统中的常见问题提供解决方案,并附源码和避坑指南,帮助用户快速上手并避免常见错误。

weixin_30485379的博客 103

从零构建:Trinamic API在STM32上的深度移植与TMC-IDE通信实战

本文详细介绍了如何从零开始在STM32平台上深度移植Trinamic API,并实现与TMC-IDE工具的稳定通信。内容涵盖SPI底层驱动定制、寄存器配置、高级功能实现及调试技巧,重点解析了TMC5160芯片的集成实践,帮助开发者构建可靠的嵌入式运动控制系统。

w1x2y3的博客 254

POJ 3414 Pots

POJ 3414 Pots题目描述 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

退役的铜牌狗 753

目标检测之Mask R-CNN(Pytorch):小白动手实现项目

目标检测之Mask R-CNN:小白动手实现项目Pytorch引言Mask R-CNN原理简述代码、注释及详细解释 引言 首先声明本项目的运行环境。操作系统为Windows,编写环境为Anaconda3 + Jupyter Notebook。Pytorch版本为 torch 1.7.1+cu110 、torchvision版本为 0.8.2+cu110,cuda版本为cu100,显卡为Nvidia 2060。 我是目标检测领域的小白,刚刚完成了Pytorch的相关入门学习任务,所以我认为学习本篇博客的基础

dzc_go的博客 1万+

【BFS】Pots

poj 3414 Pots Pots 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 fro

木又可可的博客 350

GEO的工作原理,以及GEO会催生出更多的爬虫岗位吗?

摘要: GEO(生成式引擎优化)是提升内容在AI搜索(如ChatGPT、Perplexity等)中引用率的新策略,核心在于优化内容的可抓取性、语义清晰度、权威性。与传统SEO不同,GEO聚焦AI的引用逻辑,需通过爬虫监控AI回答、逆向分析引用源特征(如结构化数据、权威背书),并构建自动化工具优化内容。爬虫工程师需转型为“AI可见性基础设施构建者”,重点关注高权威站点、知识库及官方信息源,同时确保技术可抓取性(如放行AI爬虫UA)。GEO的核心是从“排名”转向“被AI引用”,强调实体识别与动态对话适配。

数据知道的博客 4640

【温室气体】GOSAT 数据产品

【温室气体】GOSAT 数据产品

WW、forever的博客 238

POJ3414 Pots —— BFS + 模拟

题目链接:http://poj.org/problem?id=3414 Pots Time Limit:1000MS Memory Limit:65536K Total Submissions:18550 Accepted:7843 Special Judge ...

weixin_30835649的博客 156

RK3566/RK3568 Android 11添加系统服务

【代码】RK3566/RK3568 Android 11添加系统服务。

qq_23921815的博客 641

POJ3414 Pots

问题链接:POJ3414 Pots。 题意简述:给出两个壶的容量A和B, 一个目标水量C,对A、B可以有3种操作,求最少经过几步操作能够在某个壶中得到目标水量C。输入A、B和C,输入最少操作数和操作过程。 FILL(i) fill the pot i (1 ≤ i ≤ 2) from the tap;DROP(i) empty the pot...

weixin_33810006的博客 102

NTscan扫描工具

ntscan是一个扫描器,可以扫描没有密码和弱密码的共享,先到配置文件中填写猜测的用户名和密码,扫描过程中右侧出现的是扫描日志。

SIMATIC S7-1200, S7-1500 PID 控制[手册]

SIMATIC S7-1200, S7-1500 PID 控制[手册]

POJ-3414 Pots

POJ-3414 Pots 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 po

sdudyl的博客 216

锂电池充电器电路设计 里面有电路

锂电池充电器电路设计 里面有电路 3.7V升压到5V电路设计。

CAN总线超时丢包抖动?别急着报修,这是容错机制在正常工作

CAN总线作为车载与工业控制中最普及的通信协议,其可靠性并不体现在“绝不丢帧”,而是通过错误检测、错误帧、错误计数器与自动重发机制,在恶劣电磁环境下实现“错误可发现、故障可隔离”。在工程实践中,报文超时、丢包、抖动等症状,往往是系统容错机制的正常响应,而非硬件故障。理解错误重发、bus-off、位定时与采样点配置,以及应用层超时窗口和恢复策略,是定位CAN通信问题的关键。从汽车电子到嵌入式控制,合理设计容错分层与容忍带,能显著提升系统稳定性。本文基于实际调试经验,系统解析CAN超时、丢包、抖动的协议本质与排

weixin_34375251的博客 248

IMSL CNL 7.0.0 (x86-64)Crack

IMSL CNL 7.0.0 (x86-64)Crack intel的算法库破解文件,这是C++版本的破解文件

POJ 3414 Pots(罐子)

POJ3414 Pots(罐子) TimeLimit:1000MSMemoryLimit:65536K Description- 题目描述 You are given two pots, having the volume ofAandBliters respectively. The following operations can be per...

weixin_30872671的博客 81

单相智能电表AFE深度解析:从模拟前端到精度校准实践

通用ADC在电能计量中常面临动态范围不足、温漂明显和采样不同步等挑战,而专用模拟前端(AFE)正是解决这些问题的关键。AFE作为传感器与MCU之间的桥梁,通过高分辨率Δ-Σ ADC、低温漂基准和多通道同步采样,从底层保障测量精度。新一代AFE进一步将相位校正、数字积分器等DSP功能集成到芯片内部,并支持温度补偿与自校准,显著提升长期稳定性。在智能电表、功率监控、充电桩计量等场景中,AFE与分流器、互感器的协同设计,以及PCB布局和产线校准流程,往往决定了产品能否通过严苛精度测试。本文围绕单相计量AFE的架构

weixin_30600197的博客 290

【POJ 3414 --- Pots】BFS

【POJ 3414 --- Pots】BFS 题目来源:点击进入【POJ 3414 — Pots】 Description You are given two pots, having the volume of A and B liters respectively. The following operations can be performed: FILL(i) fill t...

来日浅谈的博客 521

POJ - 3414 Pots BFS + 记录并输出路径

POJ - 3414 Pots 题意: 给你两个杯子的容量 和 最后杯子中剩余水的容量。有三种操作方式 FILL(i)        将第 i 个杯子装满水 DROP(i)      将第 i 个杯子的水倒空  POUR(i,j)     将第 i 个杯子中的水倒入第 j 个杯子中     可能有两种情况:(1) i 杯子中的水还没全倒入 j 杯子中 j 杯子就满了,此时 i 杯子中还...

没有梦想何以远行 272

POJ 3414 Pots【BFS水】

POJ 3414 Pots大意:有两只桶,容量分别为a,b,对每只桶,有以下三种操作:1.Fill(i)装满第i只桶2.DROP(i)将桶i的水倒掉3.POUR(i,j),将桶i中的水倒入桶j,若桶j满,剩余的水遗留于桶i中 问能否通过这些操作使得某只桶恰好装有容量为c的水?若能,输出最少操作次数及过程PS:c<max(a,b); 算法:BFS即可 #include&...

a48634331的博客 149

倒水(BFS)Pots POJ - 3414

Pots POJ - 3414 题目链接 bfs题; 标注的状态应为2容器的体积,因为每种2容器的体积都应该只由一种状态达到。 不使用队列 #include<stdio.h> #include<string.h> struct node{ int now_x; int now_y; int step; int howcome; int before; }ll[10050];...

qq_43559193的博客 262

POJ 3414 Pots(bfs)

Pots

Pandora_heart的博客 336

POJ 3414 Pots (BFS/DFS)

Pots Time Limit:1000MS Memory Limit:65536K Total Submissions:7783 Accepted:3261 Special Judge Description You are given two pots, having the volume ofA...

airen6935的博客 190
上一篇: POJ 3026 bfs+最小生成树
下一篇: hdu 1540 神奇的线段树
左眼皮跳跳~
博客等级 码龄13年 17粉丝 400原创
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值