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. 九度oj 题目1337:寻找最长合法括号序列

    题目描述: 给你一个长度为N的,由’(‘和’)’组成的括号序列,你能找出这个序列中最长的合法括号子序列么?合法括号序列的含义便是,在这个序列中,所有的左括号都有唯一的右括号匹配:所有的右括号都有唯一的 ...

  2. zabbix的安装(一)监控os资源:内存,cpu,io,负载,带宽

    一.Linux下开源监控系统简单介绍1)cacti:存储数据能力强,报警性能差2)nagios:报警性能差,存储数据仅有简单的一段可以判断是否在合理范围内的数据长度,储存在内存中.比如,连续采样数据存 ...

  3. (转)新ITC提交APP常见问题与解决方法(Icon Alpha,Build version,AppIcon120x120)(2014-11-17)

    1)ICON无法上传,提示图片透明(有Alpha通道) 苹果现在不接受png里的Alpha了,提交的图标带有Alpha通道就提示: 简单处理:用自带的预览打开,导出时不勾选Alpha,仍保存为png格 ...

  4. BZOJ 2298: [HAOI2011]problem a【动态规划】

    Description 一次考试共有n个人参加,第i个人说:“有ai个人分数比我高,bi个人分数比我低.”问最少有几个人没有说真话(可能有相同的分数) Input 第一行一个整数n,接下来n行每行两个 ...

  5. 关于java读取文件IO流学习总结(一)

    IO流的分类: 1.根据流的数据对象来分: 高端流:所有的内存中的流都是高端流,比如:InputStreamReader 低端流:所有的外界设备中的流都是低端流,比如InputStream,Outpu ...

  6. [JSOI2008]最大数 (线段树)

    题目描述 现在请求你维护一个数列,要求提供以下两种操作: 1. 查询操作. 语法:Q L 功能:查询当前数列中末尾L个数中的最大的数,并输出这个数的值. 限制:L不超过当前数列的长度.(L>=0 ...

  7. VS2015 “GENERATERESOURCE”任务意外失败 解决方法

    昨天把项目解决方案Copy到另外的机器上执行,遭遇了一场"任务意外失败",网上搜索一下,顺利解决了,在此记录一下. Visual Studio.net 工程更换机器编译时遇到”Ge ...

  8. 无记录时显示gridview表头,并增加一行显示“没有记录”【绑定SqlDataSource控件时】

    原文发布时间为:2008-08-04 -- 来源于本人的百度文章 [由搬家工具导入] using System;using System.Data;using System.Configuration ...

  9. spring-基于注解的配置

    基于注解的配置 除了采用采用xml来配置bean之外,也可以采用注解的方式来定义,注册,加载bean. 注解的方式在spring中默认时不开启的,所以需要在xml文件中进行配置启用 注解的启动方式有下 ...

  10. iOS 设置RGB色的宏

    转自:http://lizhuang.iteye.com/blog/1931768 
//RGB Color macro #define UIColorFromRGB(rgbValue) [UICol ...