poj 3414 Pots (bfs+线索)

poj 3414 Pots (bfs+线索)

大家好,又见面了,我是全栈君,今天给大家准备了Idea注册码。

Pots
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 10071   Accepted: 4237   Special Judge

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 ≤ ≤ 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’.

Sample Input

3 5 4

Sample Output

6
FILL(2)
POUR(2,1)
DROP(1)
POUR(2,1)
FILL(2)
POUR(2,1)

思路:一共同拥有6种操作:把A中水倒掉,把A加满,把B里的水倒入A中。B和A类似。

罐子最大容积为100,设一个常量N=100, 开一个二维数组记录状态变化的值。

1、从水龙头往A里加水t,记录-t,

2、从水龙头往B里加水t,记录-t-N,

3、从B里面往A加水t,记录t

4、从A里面往B加水t。记录N+t

5、把A里水倒掉,记录2*N+t,(A原有水t)

6、把B里水倒掉,记录3*N+t,(B原有水t)

#include<stdio.h>
#include<queue>
#include<map>
#include<string>
#include<string.h>
using namespace std;
#define N 105
const int inf=0x1f1f1f1f;
int a,b,c,flag;
int mark[N][N];
struct node
{
    int x,y,t;
    friend bool operator<(node a,node b)
    {
        return a.t>b.t;
    }
};
void prif(int x,int y)       //递归输出路径
{
    if(x==0&&y==0)
        return ;
    if(mark[x][y]>3*N)
    {
        prif(x,mark[x][y]-3*N);
        printf("DROP(2)\n");
    }
    else if(mark[x][y]>2*N)
    {
        prif(mark[x][y]-2*N,y);
        printf("DROP(1)\n");
    }
    else if(mark[x][y]>N)
    {
        int tmp=mark[x][y]-N;
        prif(x+tmp,y-tmp);
        printf("POUR(1,2)\n");
    }
    else if(mark[x][y]>0)
    {
        int tmp=mark[x][y];
        prif(x-tmp,y+tmp);
        printf("POUR(2,1)\n");
    }
    else if(mark[x][y]>-N)
    {
        int tmp=-mark[x][y];
        prif(x-tmp,y);
        printf("FILL(1)\n");
    }
    else if(mark[x][y]<-N)
    {
        int tmp=N+mark[x][y];
        prif(x,y+tmp);
        printf("FILL(2)\n");
    }
}
void bfs()
{
    priority_queue<node>q;
    node cur,next;
    mark[0][0]=inf;  //该状态仅仅能出现一次。赋值为inf防止干扰其它值
    mark[a][0]=-a;
    mark[0][b]=-b-N;
    cur.t=1;       
    cur.x=a;
    cur.y=0;
    q.push(cur);
    cur.x=0;
    cur.y=b;
    q.push(cur);
    while(!q.empty())
    {
        cur=q.top();
        q.pop();
        next.t=cur.t+1;
        if(cur.x==c||cur.y==c)
        {
            flag=1;
            printf("%d\n",cur.t);
            prif(cur.x,cur.y);
            return ;
        }
        if(cur.x<a)       //向A加水
        {
            int tmp=a-cur.x;
            next.y=cur.y;
            next.x=a;         //来自水龙头的水
            if(!mark[next.x][next.y])
            {
                mark[next.x][next.y]=-tmp;
                q.push(next);
            }
            if(cur.y>0)      //来自B的水
            {
                int tmp=min(cur.y,a-cur.x);
                next.x=cur.x+tmp;
                next.y=cur.y-tmp;
                if(!mark[next.x][next.y])
                {
                    mark[next.x][next.y]=tmp;
                    q.push(next);
                }
            }
        }
        if(cur.y<b)        //向B加水
        {
            int tmp=b-cur.y;
            next.x=cur.x;
            next.y=b;      //来自水龙头的水
            if(!mark[next.x][next.y])
            {
                mark[next.x][next.y]=-tmp-N;
                q.push(next);
            }
            if(cur.x>0)      //来自A的水
            {
                int tmp=min(cur.x,b-cur.y);
                next.y=cur.y+tmp;
                next.x=cur.x-tmp;
                if(!mark[next.x][next.y])
                {
                    mark[next.x][next.y]=tmp+N;
                    q.push(next);
                }
            }
        }
        if(cur.x>0)     //倒掉水
        {
            int tmp=cur.x;
            next.x=0;
            next.y=cur.y;
            if(!mark[next.x][next.y])
            {
                mark[next.x][next.y]=2*N+tmp;
                q.push(next);
            }
        }
        if(cur.y>0)
        {
            int tmp=cur.y;
            next.y=0;
            next.x=cur.x;
            if(!mark[next.x][next.y])
            {
                mark[next.x][next.y]=3*N+tmp;
                q.push(next);
            }
        }
    }
}
int main()
{
    while(scanf("%d%d%d",&a,&b,&c)!=-1)
    {
        memset(mark,0,sizeof(mark));
        flag=0;
        bfs();
        if(!flag)
            printf("impossible\n");
    }
    return 0;
}

版权声明:本文博客原创文章,博客,未经同意,不得转载。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/117660.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 计算机中1kb等于多少字节,1mb等于多少kb「建议收藏」

    计算机中1kb等于多少字节,1mb等于多少kb「建议收藏」⑴计算机的储存容量1MB等于多少KB计算机的储存容量1MB=1024KB。MB,为英文“MByte”的简写,是计算机中的一种储存单位,读作“兆”。存储容量是指存储器可以容纳的二进制信息量,用存储器中存储地址寄存器MAR的编址数与存储字位数的乘积表示。网络上的所有信息都是以“位”(bit)为单位传递的,一个位就代表一个0或1。(1)1mb等于多少kb扩展阅读存储容量是便携存储产品最为关键的参数…

    2022年5月25日
    73
  • linux内核编程接口小结(一)

    linux内核编程接口小结(一)1、下面两幅图描述了linux内核编程接口函数

    2022年10月8日
    0
  • 通达OA工作流-流程设计

    通达OA工作流-流程设计2.2    流程设计  2.2.1    流程分类  在工作流工作流设置分类设置可以为系统添加流程分类。流程分类更方便了流程的管理,把不同性质的流程放在不同的分类下,也方便了流程的查找。 同时根据流程分类的所属部门,实现了流程分类按部门进行独立管理的目的。

    2022年6月23日
    33
  • ORACLE备份恢复

    ORACLE备份恢复目录一、关于备份与恢复 二、逻辑备份(expdp和impdp) 三、物理备份 四、数据库日常备份计划及脚本参考一、关于备份与恢复1、备份定义备份就是把数据库复制到转储设备的过程。其中,转储设备是指用于放置数据库副本的磁带或磁盘。通常也将存放于转储设备中的数据库的副本称为原数据库的备份或转储。备份是一份数据副本2、备份分类从物理与逻辑的角度来分类:从物理与逻辑的,备份可以分为物理备份和逻辑备份。物理备份:对数据库操作系统的物理文件(数据文件,控制文件和日志文件)的备份。物…

    2022年7月17日
    19
  • Σ求和符号_西格玛符号怎么打

    Σ求和符号_西格玛符号怎么打转自:https://zh.wikipedia.org/wiki/%E6%B1%82%E5%92%8C%E7%AC%A6%E5%8F%B7求和符号(Σ,sigma),是欧拉于1755年首先使用的。这个符号是源于希腊文σογμαρω(增加)的字头,Σ正是σ的大写。求和的结果是给定的数值相加后的总值,又称加总。举例而言,若有4个数值:1、3、5、7,则这4个数值的总和为:

    2022年10月9日
    0
  • 多层感知器神经网络实例_多层感知器与bp神经网络

    多层感知器神经网络实例_多层感知器与bp神经网络作者|VivekPatel编译|Flin来源|towardsdatascience除非你能学习到一些东西,否则不要重复造轮子。强大的库已经存在了,如:TensorFlow,PyTorch,Keras等等。我将介绍在Python中创建多层感知器(MLP)神经网络的基本知识。感知器是神经网络的基本组成部分。感知器的输入函数是权重,偏差和输入数据的线性组合。具体来说:in_j=weightinput+bias.(in_j=权重输入+偏差)。在每个感知器上,我们都可以指定一个激活函数g。

    2022年8月30日
    0

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号