Pots
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 11661   Accepted: 4940   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)

题意是每次对面前的两个壶有六种选择,倒掉1壶的水,倒掉2壶的水。填满1壶的水,填满2壶的水。把2壶的水倒入1壶,把1壶的水倒入2壶。

然后求想要达到结果C毫升水的最小路径,并输出这个路径。

这个题目形式又是 选择+最小路径 。广度优先搜索无疑。

代码:

#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#pragma warning(disable:4996)
using namespace std; queue <int> pot1;
queue <int> pot2; int buzhou[105][105];
string path[105][105];// drop[1] drop[2] fill[1] fill[2] pour[2,1] pour[1,2] int main()
{
//freopen("i.txt","r",stdin);
//freopen("o.txt","w",stdout); int A,B,C,flag,v1,v2,i;
cin>>A>>B>>C; memset(buzhou,0,sizeof(buzhou)); pot1.push(0);
pot2.push(0); flag=0;
v1=0;
v2=0;
buzhou[v1][v2]=1; while(pot1.size()&&pot2.size())
{
v1=pot1.front();
v2=pot2.front(); pot1.pop();
pot2.pop(); if(v1==C||v2==C)
{
flag=1;
cout<<buzhou[v1][v2]-1<<endl;
for(i=0;i<path[v1][v2].size();i++)
{
if(path[v1][v2][i]=='1')
cout<<"DROP(2)"<<endl;
else if(path[v1][v2][i]=='2')
cout<<"DROP(1)"<<endl;
else if(path[v1][v2][i]=='3')
cout<<"FILL(1)"<<endl;
else if(path[v1][v2][i]=='4')
cout<<"FILL(2)"<<endl;
else if(path[v1][v2][i]=='5')
cout<<"POUR(2,1)"<<endl;
else if(path[v1][v2][i]=='6')
cout<<"POUR(1,2)"<<endl;
}
break;
}
if(!buzhou[0][v2])//drop1
{
buzhou[0][v2]=buzhou[v1][v2]+1;
path[0][v2] = path[v1][v2]+"2";
pot1.push(0);
pot2.push(v2);
}
if(!buzhou[v1][0])//drop2
{
buzhou[v1][0]=buzhou[v1][v2]+1;
path[v1][0] = path[v1][v2]+"1";
pot1.push(v1);
pot2.push(0);
}
if(!buzhou[v1][B])//fill2
{
buzhou[v1][B]=buzhou[v1][v2]+1;
path[v1][B] = path[v1][v2]+"4";
pot1.push(v1);
pot2.push(B);
}
if(!buzhou[A][v2])//fill1
{
buzhou[A][v2]=buzhou[v1][v2]+1;
path[A][v2] = path[v1][v2]+"3";
pot1.push(A);
pot2.push(v2);
} if(v1+v2<A)//b to a
{
if(!buzhou[v1+v2][0])
{
buzhou[v1+v2][0]=buzhou[v1][v2]+1;
path[v1+v2][0]= path[v1][v2]+"5";
pot1.push(v1+v2);
pot2.push(0);
}
}
else
{
if(!buzhou[A][v1+v2-A])
{
buzhou[A][v1+v2-A]=buzhou[v1][v2]+1;
path[A][v1+v2-A]= path[v1][v2]+"5";
pot1.push(A);
pot2.push(v1+v2-A);
}
} if(v1+v2<B)
{
if(!buzhou[0][v1+v2])
{
buzhou[0][v1+v2]=buzhou[v1][v2]+1;
path[0][v1+v2]= path[v1][v2]+"6";
pot1.push(0);
pot2.push(v1+v2);
}
}
else
{
if(!buzhou[v1+v2-B][B])
{
buzhou[v1+v2-B][B]=buzhou[v1][v2]+1;
path[v1+v2-B][B]= path[v1][v2]+"6";
pot1.push(v1+v2-B);
pot2.push(B);
}
}
}
if(!flag)
cout<<"impossible"<<endl;
system("pause");
return 0;
}

POJ 3414:Pots的更多相关文章

  1. 【POJ - 3414】Pots(bfs)

    Pots 直接上中文 Descriptions: 给你两个容器,分别能装下A升水和B升水,并且可以进行以下操作 FILL(i)        将第i个容器从水龙头里装满(1 ≤ i ≤ 2); DRO ...

  2. POJ 3414 Pots

    Pots Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status  ...

  3. POJ 3414 Pots(罐子)

    POJ 3414 Pots(罐子) Time Limit: 1000MS    Memory Limit: 65536K Description - 题目描述 You are given two po ...

  4. poj 3414 Pots 【BFS+记录路径 】

    //yy:昨天看着这题突然有点懵,不知道怎么记录路径,然后交给房教了,,,然后默默去写另一个bfs,想清楚思路后花了半小时写了120+行的代码然后出现奇葩的CE,看完FAQ改了之后又WA了.然后第一次 ...

  5. BFS POJ 3414 Pots

    题目传送门 /* BFS:六种情况讨论一下,BFS轻松解决 起初我看有人用DFS,我写了一遍,TLE..还是用BFS,结果特判时出错,逗了好长时间 看别人的代码简直是受罪,还好自己终于发现自己代码的小 ...

  6. Pots(POJ - 3414)【BFS 寻找最短路+路径输出】

    Pots(POJ - 3414) 题目链接 算法 BFS 1.这道题问的是给你两个体积分别为A和B的容器,你对它们有三种操作,一种是装满其中一个瓶子,另一种是把其中一个瓶子的水都倒掉,还有一种就是把其 ...

  7. 广搜+输出路径 POJ 3414 Pots

    POJ 3414 Pots Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13547   Accepted: 5718   ...

  8. Pots POJ 3414

    /* *POJ 3414 *简单模板bfs *编程应该为了方便理解,尽量提供接口 */ #include<cstdio> #include<algorithm> #includ ...

  9. 【BFS】POJ 3414

    直达 -> POJ 3414 Pots 相似题联动–>HDU 1495 非常可乐 题意:两个壶倒水,三种操作,两个桶其中一个满足等于C的最少操作,输出路径.注意a,b互倒的时候能不能倒满, ...

随机推荐

  1. shell脚本举例

    1.有时在写一些以循环方式运行的监控脚本,设置时间间隔是必不可少的,下面是一个Shell进度条的脚本演示在脚本中生成延时. #!/bin/bash b='' for ((i=0;$i<=100; ...

  2. spring junit4 单元测试运行正常,但是数据库并无变化

    解决方案 http://blog.csdn.net/molingduzun123/article/details/49383235 原因:Spring Juint为了不污染数据,对数据的删除和更新操作 ...

  3. git2--常用命令

    Git 命令详解及常用命令 Git作为常用的版本控制工具,多了解一些命令,将能省去很多时间,下面这张图是比较好的一张,贴出了看一下: 关于git,首先需要了解几个名词,如下: ? 1 2 3 4 Wo ...

  4. CodeForces 321C Ciel the Commander

    Ciel the Commander Time Limit: 1000ms Memory Limit: 262144KB This problem will be judged on CodeForc ...

  5. 【Kubernetes】离线业务:Job与CronJob

    Deployment.StatefulSet和DaemonSet这三个编排概念编排的对象主要都是在线业务(Long Running Task,这些应用一旦运行起来,除非出错或者停止,它的容器进程会一直 ...

  6. 安卓ImageView.src设置图片拉伸、填满控件的方法

    代码改变世界 安卓ImageView.src设置图片拉伸.填满控件的方法 需要给你的ImageView布局加上Android:adjustViewBounds="true"

  7. UITableView性能-圆角图片

    圆角图片因为GPU渲染会影响性能 参考:http://www.cocoachina.com/ios/20150803/12873.html http://blog.sina.com.cn/s/blog ...

  8. BZOJ 3926 [Zjoi2015]诸神眷顾的幻想乡 ——广义后缀自动机

    神奇的性质,叶子节点不超过20个. 然后把这些节点提出来构成一颗新树,那么这些树恰好包含了所有的情况. 所以直接广义后缀自动机. 然后统计本质不同的字符串就很简单显然了. #include <c ...

  9. 设置好uTorrent让你的下载速度飞起来

    由于有会员反映下载国外种子速度很慢的问题,而我下同样的种子,竟然那天下载最高速度能到500K/秒.(我用的是移动的校园网,这种出了名的烂网,十天有七天是图片都打不开的网)这可见是所用软件和软件的设置问 ...

  10. 云计算与 OpenStack

    “云计算” 算是近年来最热的词了.现在 IT 行业见面不说这三个字您都不好意思跟人家打招呼. 对于云计算,学术界有各种定义,大家有兴趣可以百度一下. CloudMan 这里主要想从技术的角度谈谈对云计 ...