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)
上一篇 2022年1月2日 上午11:00
下一篇 2022年1月2日 上午11:00


相关推荐

  • webpack-ES6转ES5[通俗易懂]

    webpack-ES6转ES5[通俗易懂]@webpack-ES6转ES5的babel-loader安装babel-loader:npminstall–savedevbabel-loader@7babel-corebabel-preset-es2015用法:在webpack配置对象中,需要将babel-loader添加到module列表中module:{rules:[{test:/\.m?js$/,exclude:/(node_modules|bower_c

    2025年12月10日
    5
  • tinyint 范围「建议收藏」

    tinyint 范围「建议收藏」最进做项目要记日志日志表同事建的关联任务id用的tinyint一开始测试没问题后来日志记录里数据全是127纳闷看了127的也没人使用然后才看到“`lang=sqlTINYINT型的字段如果不设置UNSIGNED类型,存储-128到127的整数。“`改了就好了抠鼻.jpg…

    2026年2月11日
    5
  • 缅怀金庸 || 这些年你打过的金庸游戏

    缅怀金庸 || 这些年你打过的金庸游戏微信公众号 酷玩区块 飞雪连天射白鹿 笑书神侠倚碧鸳 这是金庸老先生小说作品首字母连起来 形成的一句朗朗上口的诗句 尽管我们还没有从 2018 年 10 月 25 日年仅 50 岁的李咏就因为癌症英年早逝命运无常的感叹中走出来 2018 年 10 月 30 日晚 18 55 分 据港媒报道 武侠小说泰斗作家查良镛 笔名金庸 逝世于香港养和医院 享年 94 岁 人们纷纷哀痛并发文悼念 金庸老先生生前的作品受到了几代

    2026年3月19日
    2
  • quartus ii安装教程9.0激活成功教程教程_quartus ii 13.1安装教程

    quartus ii安装教程9.0激活成功教程教程_quartus ii 13.1安装教程一、首先是QuartusII13.0.1软件的下载如果你没有那么高的要求,用个低版本的QuartusII就足够了,而且低版本的软件比较稳定,为了免去大家找安装文件版本号不匹配的情况,我在这里把我所用的QuartusII13.0.1版本的源安装文件、激活成功教程文件和器件库(Cyclone,CycloneII,CycloneIII,CycloneIVdevices…

    2022年10月15日
    6
  • R语言画图——添加数学表达式和R2[通俗易懂]

    R语言画图——添加数学表达式和R2代码如下:filepath<-file.choose()df1<-read.csv(filepath,header=T)df1library(ggplot2)QTs<-ggplot(data=df1,aes(x=Ts,y=Q10,shape=factor))+geom_point(size=3)+scale_shape_manual(values=c(1,17))+#白天

    2022年4月17日
    39
  • 遗传算法代码

    遗传算法代码全局搜索最优算法 1 遗传算法这里以 github 上的遗传算法开源库为例子 首先我们安装 GA 官方说依赖库好像只支持 Python3 但是我好像 python2 也安装成功了 pip3installp 在这里我们讨论一个简单的全局优化过程 讨论 x 2 2 y 4 2 x 2 2 y 4 2 x 2 2 y 4 2 在 x 1 5 y 6 9 x subset 1 5 y subset 6 9 x 1 5 y 6 9 的最大值 源码如下 importnump

    2026年3月18日
    2

发表回复

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

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