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


相关推荐

  • 2套后台模板HTML+整套Easyui皮肤组件-后台管理系统模板

    2套后台模板HTML+整套Easyui皮肤组件-后台管理系统模板2019年最新easyui主题模板设计:http://www.uimaker.com/easyui本作品仅供学习参考,请勿用于任何商业用途,版权所有:uimaker.com,谢绝任何网站转载,请互相理解!设计业务联系QQ:32534386请注:模板说明:由于效果图比较多,合并成一个图片文件后,文件很大,所以进行了压缩,导致您看到的效果图都比较灰,其实…

    2025年10月28日
    4
  • MATLAB GUI的运行原理理解

    MATLAB GUI的运行原理理解MATLABGUI原理的一些个人理解

    2022年5月20日
    34
  • 华中农业大学python实验题

    华中农业大学python实验题华中农业大学Python部分实验题,旨在为大家提供思路,希望大家抱着借鉴的心理来学习,不要直接抄袭。

    2022年7月11日
    15
  • 关于Android大数据收集,埋点统计的详细讲解以及案例代码分析附github代码

    关于Android大数据收集,埋点统计的详细讲解以及案例代码分析附github代码关于Android大数据收集,埋点统计的详细讲解以及案例代码分析附github代码一、背景分析目前大数据的分析对一款成熟的APP来说至关重要,特别是商业性的APP和金融类的APP都会对用户的行为进行分析,所以在APP中集成大数据的收集就显得很重要。目前来说,第三方的数据收集也挺多的,像是友盟,AOP切面收集等等,但是他们就是简单的集成,如果说在某些极端的情况下,项目中禁止添加额外的辅助,例

    2022年5月18日
    44
  • CefSharp之二–如何看懂demo中的例子,以及按照例子进行开发「建议收藏」

    CefSharp之二–如何看懂demo中的例子,以及按照例子进行开发「建议收藏」CefSharp是做什么用的?请看前一篇文章:怎么用c#编写浏览器或者执行javascript代码?之后就是如何开发了。这个CefSharp最坑的是,还早不到文档,只能看着官方给的例子开发。项目地址那么就可以看到,带有example的都是例子。我给大家举2个例子。1.事件添加:我想让我的程序出了网页上的js代码,再额外的执行我自己写的js,怎么办呢?InitializeCom

    2026年1月23日
    6
  • github开发人员在七夕搞事情:remote: Support for password authentication was removed on August 13, 2021.

    github开发人员在七夕搞事情:remote: Support for password authentication was removed on August 13, 2021.1问题描述如果你在七夕(没错就是2020年8月14日)的这一天刚好加班,又刚好去访问了全球最大的同性交友网站,又刚好去更新提交代码,又或你创建了一个新的仓库送给自己,又刚好想把这个仓库送给(push)github,你就刚好会遇到这个问题:remote:SupportforpasswordauthenticationwasremovedonAugust13,2021.Pleaseuseapersonalaccesstokeninstead.具体如下:(yolov4)

    2022年7月20日
    29

发表回复

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

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