Pots

Stella981
• 阅读 376

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 jis 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)题解

还是BFS

设(i,j)为瓶1与瓶2在某一时刻的容量,那么从这点出发,可以到达的点有:

(A, j) : FILL(1)

(i, B) : FILL(2)

(0, j): DROP(1)

(i, 0): DROP(2)

(i+j, 0) or (A, j-A+i) : POUR(2,1)

(0, i+j) or (i-B+j, B) : POUR(1,2)

判断这些点的访问状态,然后广搜下去,另外,要求出从源顶点到这些点的最短路径

当i=C 或 j=C时,就找到了

同时,为了直到到达(i, j)这一点时,所做的操作,我还设了一个operation变量,来记录达到一点所做的操作,供最后输出

在题中,我用的是一维数组,因此(i, j)变为(i*(B+1))+j

print_path函数,实质上输出了从源顶点到目标点的最短路径上的每个点的操作。

 

原文:https://blog.csdn.net/kindlucy/article/details/5827794

Pots Pots

#include <iostream>
#include <queue>
using namespace std;

int *color;
int *pi;
int *d;
int *operation; //保存操作

int BFS(int A,int B,int C)
{
    if(C==0)
        return 0;
    int num[2]={A,B};
    int temp=(A+1)*(B+1);
    color=new int[temp];
    pi=new int[temp];
    d=new int[temp];
    operation=new int[temp];
    for(int i=0;i<temp;i++)
    {
        color[i]=0;
        pi[i]=0;
        d[i]=0;
        operation[i]=0;
    }
    color[0]=1;//(0,0)
    d[0]=0;
    queue<int> Q;
    Q.push(0);
    while(!Q.empty())
    {
        int u=Q.front();
        Q.pop();
        int v[2];
        v[0]=u/(B+1);//瓶A中的水容量
        v[1]=u-v[0]*(B+1);//瓶B中的水容量
        int i; //经过操作后,新的容量(i/(B+1),i%(B+1))
        for(int j=0;j<6;j++)
        {
            switch(j)
            {
            case 0:
                i=num[0]*(B+1)+v[1];        //FILL(1)
                break;
            case 1:
                i=v[0]*(B+1)+num[1];        //FILL(2)
                break;
            case 2:
                i=v[1];                        //DROP(1)
                break;
            case 3:
                i=v[0]*(B+1);                //DROP(2)
                break;
            case 4:
                if(v[0]+v[1]<=num[0])
                    i=(v[0]+v[1])*(B+1);
                else
                    i=(num[0]*(B+1))+v[1]-(num[0]-v[0]);
                break;                        //POUR(2,1)
            case 5:
                if(v[0]+v[1]<=num[1])
                    i=(v[0]+v[1]);
                else
                    i=(v[0]-(num[1]-v[1]))*(B+1)+num[1];
                break;                        //POUR(1,2)
            default:
                break;
            }
            if(color[i]==0)
            {
                d[i]=d[u]+1;
                color[i]=1;
                pi[i]=u;
                operation[i]=j;
                if(i/(B+1)==C || (i%(B+1))==C)
                    return i;
                Q.push(i);
            }
        }
        color[u]=2;
    }
    return -1;
}

void print_path(int s,int v)
{
    if(v==s)
        return;
    print_path(s,pi[v]);
    switch(operation[v])
    {
    case 0:
        cout << "FILL(1)";
        break;
    case 1:
        cout << "FILL(2)";
        break;
    case 2:
        cout << "DROP(1)";
        break;
    case 3:
        cout << "DROP(2)";
        break;
    case 4:
        cout << "POUR(2,1)";
        break;
    case 5:
        cout << "POUR(1,2)";
        break;
    default:
        break;
    }
    cout << endl;
}

int main()
{
    int A,B,C;
    cin >> A >> B >> C;
    int output=BFS(A,B,C);
    if(output!=-1)
    {
        cout << d[output] << endl;
        print_path(0,output);
    }
    else
        cout << "impossible" << endl;
}

View Code

点赞
收藏
评论区
推荐文章
blmius blmius
2年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
Wesley13 Wesley13
2年前
java将前端的json数组字符串转换为列表
记录下在前端通过ajax提交了一个json数组的字符串,在后端如何转换为列表。前端数据转化与请求varcontracts{id:'1',name:'yanggb合同1'},{id:'2',name:'yanggb合同2'},{id:'3',name:'yang
Jacquelyn38 Jacquelyn38
2年前
2020年前端实用代码段,为你的工作保驾护航
有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )
皕杰报表之UUID
​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为
Stella981 Stella981
2年前
KVM调整cpu和内存
一.修改kvm虚拟机的配置1、virsheditcentos7找到“memory”和“vcpu”标签,将<namecentos7</name<uuid2220a6d1a36a4fbb8523e078b3dfe795</uuid
Wesley13 Wesley13
2年前
mysql设置时区
mysql设置时区mysql\_query("SETtime\_zone'8:00'")ordie('时区设置失败,请联系管理员!');中国在东8区所以加8方法二:selectcount(user\_id)asdevice,CONVERT\_TZ(FROM\_UNIXTIME(reg\_time),'08:00','0
Stella981 Stella981
2年前
JS 对象数组Array 根据对象object key的值排序sort,很风骚哦
有个js对象数组varary\{id:1,name:"b"},{id:2,name:"b"}\需求是根据name或者id的值来排序,这里有个风骚的函数函数定义:function keysrt(key,desc) {  return function(a,b){    return desc ? ~~(ak
Wesley13 Wesley13
2年前
00:Java简单了解
浅谈Java之概述Java是SUN(StanfordUniversityNetwork),斯坦福大学网络公司)1995年推出的一门高级编程语言。Java是一种面向Internet的编程语言。随着Java技术在web方面的不断成熟,已经成为Web应用程序的首选开发语言。Java是简单易学,完全面向对象,安全可靠,与平台无关的编程语言。
Wesley13 Wesley13
2年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
Python进阶者 Python进阶者
3个月前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这