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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • android获取屏幕像素密度DPI,与density

    android获取屏幕像素密度DPI,与density基本概念:dip:Densityindependentpixels,设备无关像素。dp:就是dipdpi:dotsperinch,直接来说就是一英寸多少个像素点。常见取值120,160,240。我一般称作像素密度,简称密度density:直接翻译的话貌似叫密度。常见取值1.5,1.0。和标准dpi的比例(160px

    2022年6月3日
    37
  • 精确度 召回率 f1_score多大了

    精确度 召回率 f1_score多大了目录混淆矩阵准确率精确率召回率分类是机器学习中比较常见的任务,对于分类任务常见的评价指标有准确率(Accuracy)、精确率(Precision)、召回率(Recall)、F1score、ROC曲线(ReceiverOperatingCharacteristicCurve)等。这篇文章将结合sklearn对准确率、精确率、召回率、F1score进行讲解,ROC曲线可以参考我的这篇文章…

    2022年10月15日
    0
  • 怎么创建数据表_sqlserver创建表的sql语句

    怎么创建数据表_sqlserver创建表的sql语句知识点:数据库表的相关概念、创建数据库表的方法、设计数据库表、向数据库表中插入数据、建立不同数据库表之间的关系、删除数据库表。1、数据表相关的一些概念1.1数据库里的数据是如何保存的?数据库到底是怎么存储数据的?比如要把学生信息存储到数据库里,能把学生塞进数据库吗?肯定是把学生的数据信息抽象出来,把一些重要信息以文字或数字的形式保存到数据库中去。…

    2022年10月25日
    0
  • 常用网管工具_网管系统

    常用网管工具_网管系统1、ping(你懂的)2、df-h(查看磁盘使用情况)3、top(查看cpu使用情况,按“1”可以查看各个cpu详情)4、pstree(树形显示进程及同名进程的数量)5、vmstat[m][n](显示服务器的CPU使用率,内存使用,虚拟内存交换情况,IO读写情况。间隔m秒钟显示一次,一共显示n次)6、nload(监视网络吞吐量)7、ps…|grep

    2022年10月6日
    0
  • 视频服务器搭建流媒体_个人服务器搭建

    视频服务器搭建流媒体_个人服务器搭建
    什么样的情况下才使用FMS?有以下几种情形的时候,你可能需要用到FMS
    1、需要通过FlashPlayer播放视频,而视频是以流的方式,而不是http渐进式下载的方式进行播放的时候。渐进式下载就是仍然走http协议,youtube,土豆等站点就是。那么什么时候才真正需要用到流视频呢?
    1)视频文件超过100MB或超过10分钟,用户有seek需求的时候
    2)视频文件不需要被下载的时候,rtmp如果不行,可以试试rtmpe
    3)视频流需要多

    2022年10月20日
    0
  • 【转载】什么是堆和栈,它们在哪儿?

    【转载】什么是堆和栈,它们在哪儿?

    2021年11月20日
    42

发表回复

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

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