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. Java实现——字符串分割以及复制目录下的所有文件

    0.  前言 今天有个需求,把Android中data/data目录下指定(通过读预置的XML文件)的多个应用下的多个目录全部内容通过OTG模式复制到U盘中. 首先读取XML文件内的某个节点的属性值, ...

  2. PYDay7&8-递归、冒泡算法、装饰器

    1.登录验证代码 1.1纯登录验证-函数实现 def login(username,password): ''' 用于用户名密码的验证 :param username: 用户名 :param pass ...

  3. xfce-openvas9

    1安装OpenVas 第一步,添加PPA源,在这我用的是一台新装的Ubuntu安装OpenVas,运行以下命令就可以进行安装 root@ubuntu:~# add-apt-repository ppa ...

  4. 【LeetCode】Integer to Roman(整数转罗马数字)

    这道题是LeetCode里的第12道题. 吐了,刚做完"罗马数字转整数",现在又做这个.这个没什么想法,只能想到使用if语句嵌套,或者使用哈希表.但哈希表我还不熟练啊.先拿if嵌套 ...

  5. 九度oj 题目1207:质因数的个数

    题目描述: 求正整数N(N>1)的质因数的个数. 相同的质因数需要重复计算.如120=2*2*2*3*5,共有5个质因数. 输入: 可能有多组测试数据,每组测试数据的输入是一个正整数N,(1&l ...

  6. Android点击按钮拨打电话

    代码改变世界 Android点击按钮拨打电话 public void callPhone(String str) { Intent intent=new Intent(); intent.setAct ...

  7. Python2.6.6升级2.7.3

    Python2.7替换2.6: 1.下载Python-2.7.3 #wget http://python.org/ftp/python/2.7.3/Python-2.7.3.tar.bz2 2.解压 ...

  8. 【Luogu】P1607庙会班车Fair Shuttle(线段树+贪心)

    我不会做贪心题啊……贪心题啊……题啊……啊…… 我真TM菜爆了啊…… 这题就像凌乱的yyy一样,把终点排序,终点相同的按起点排序.然后维护一个查询最大值的线段树.对于一个区间[l,r],如果这个区间已 ...

  9. SPOJ LCS2 Longest Common Substring II ——后缀自动机

    后缀自动机裸题 #include <cstdio> #include <cstring> #include <iostream> #include <algo ...

  10. BZOJ 3990 [SDOI2015]排序 ——搜索

    [题目分析] 可以发现,操作的先后顺序是不影响结果的,那么答案就是n!的和. 可以从小的步骤开始搜索,使得每一个当前最小的块都是上升的数列,然后看看是否可行即可. 复杂度好像是4^n [代码](哪里写 ...