转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents

题目链接:

id=3414">http://poj.org/problem?

id=3414

此题和poj1606一样 :http://blog.csdn.net/u012860063/article/details/37772275

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)FILL(i),将i水壶的水填满;

2)DROP(i)。将水壶i中的水所有倒掉;

3)POUR(i,j)将水壶i中的水倒到水壶j中。若水壶 j 满了,则 i 剩下的就不倒了,问进行多少步操作。而且怎么操作,输出操作的步骤,两个水壶中的水能够达到C这个水量。假设不可能则输出impossible

初始时两个水壶是空的,没有水。

代码例如以下:

#include <iostream>
#include <algorithm>
using namespace std;
#include <cstring>
#include <queue>
#include <stack>
struct cup
{
int x, y;
int step;
int flag;//标记操作
cup *pre;//记录路径
};
queue<cup>Q;
stack<int>R;
int a, b, e;
int vis[117][117]={0};//标记当前状态是否到达过
int ans;
void BFS(int x, int y)
{
cup c;
cup t[317];//眼下瓶子里剩余的水量
c.x = 0, c.y = 0;
c.flag = 0;
c.pre = NULL;
c.step = 0;
Q.push(c);
vis[x][y] = 1;
int count = -1;
while(!Q.empty())
{
count++;
t[count] = Q.front();
Q.pop();
for(int i = 1; i <= 6; i++)
{
switch(i)
{
case 1: //fill a
c.x = a;
c.y = t[count].y;
c.flag = 1;
break;
case 2: //fill b
c.x = t[count].x;
c.y = b;
c.flag = 2;
break;
case 3: //drop a
c.x = 0;
c.y = t[count].y;
c.flag = 3;
break;
case 4: //drop b
c.x = t[count].x;
c.y = 0;
c.flag = 4;
break;
case 5: //pour a to b
if(t[count].x > b-t[count].y)
{
c.x = t[count].x-(b-t[count].y);
c.y = b;
}
else
{
c.x = 0;
c.y = t[count].y+t[count].x;
}
c.flag = 5;
break;
case 6: //pour b to a
if(t[count].y > a-t[count].x)
{
c.y = t[count].y - (a-t[count].x);
c.x = a;
}
else
{
c.x = t[count].x+t[count].y;
c.y = 0;
}
c.flag = 6;
break;
}
if(vis[c.x][c.y])
continue;
vis[c.x][c.y] = 1;
c.step = t[count].step+1;
c.pre = &t[count];
if(c.x == e || c.y == e)
{
ans = c.step;
while(c.pre)
{
R.push(c.flag);
c = *c.pre;
}
return;
}
Q.push(c);
}
}
}
void print()
{
while(!R.empty())
{
int i = R.top();
R.pop();
switch(i)
{
case 1:cout<<"FILL(1)"<<endl;break;
case 2:cout<<"FILL(2)"<<endl;break;
case 3:cout<<"DROP(1)"<<endl;break;
case 4:cout<<"DROP(2)"<<endl;break;
case 5:cout<<"POUR(1,2)"<<endl;break;
case 6:cout<<"POUR(2,1)"<<endl;break;
}
}
}
int main()
{
cin >>a>>b>>e;
BFS(0,0);
if(ans == 0)
cout<<"impossible"<<endl;
else
{
cout<<ans<<endl;
print();
}
return 0;
}

poj 3414 Pots(广搜BFS+路径输出)的更多相关文章

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

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

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

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

  3. BFS POJ 3414 Pots

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

  4. POJ 3414 Pots(罐子)

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

  5. poj 3414 Pots【bfs+回溯路径 正向输出】

    题目地址:http://poj.org/problem?id=3414 Pots Time Limit: 1000MS   Memory Limit: 65536K Total Submissions ...

  6. [POJ] 1606 Jugs(BFS+路径输出)

    题目地址:http://poj.org/problem?id=1606 广度优先搜索的经典问题,倒水问题.算法不需要多说,直接BFS,路径输出采用递归.最后注意是Special Judge #incl ...

  7. POJ 3126 Prime Path 简单广搜(BFS)

    题意:一个四位数的质数,每次只能变换一个数字,而且变换后的数也要为质数.给出两个四位数的质数,输出第一个数变换为第二个数的最少步骤. 利用广搜就能很快解决问题了.还有一个要注意的地方,千位要大于0.例 ...

  8. POJ 3414 Pots

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

  9. hdu 1253:胜利大逃亡(基础广搜BFS)

    胜利大逃亡 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submi ...

随机推荐

  1. Android:EditText 属性

    Android开发EditText属性 EditText 官网链接 EditText继承关系:View-->TextView-->EditText EditText 部分属性: XML 设 ...

  2. CDH5.2+CM5.2+impala2+Spark1.1 集群搭建基础环境准备

    測试集群简单介绍:一共同拥有4台机器:10.10.244.136.10.10.244.137.10.10.244.138.10.10.244.139. 10.10.244.136是管理节点.另外3台是 ...

  3. IT人都非常忙(茫)

    我发现.身边的盆友都非常忙,要么在加班.要么加班刚回家:要么在出差,要么刚出差回来. 难道搞IT的人都非常忙么?忙还是茫? 大学期间.不知道未来要干什么.非常多人也不清楚应该学习哪些知识和技能.是否须 ...

  4. C++ 智能指针 shared_ptr

    今天晚上去旁听了C++高级编程的课,其中提到智能指针.第一反映还以为是auto_ptr呢,一听才知道是share_ptr这个.哦,原来是C++11特性.大致的原因是auto_ptr有一点缺陷,而sha ...

  5. Mysql 中创建索引和索引的使用问题

    在数据库中合理的使用索引是提升mysql数据库的一种高效和快捷的方式,但是在索引的使用上在我的使用中发现有很多坑,因为自己之前没有认识到,所以来总结一下 索引的介绍 索引是一种特殊的文件,其中包含着对 ...

  6. Solr学习之五

    一.段管理 段是一个自包含,仅可读的solr的索引的子集.一旦一个段被刷新到持久存储后,它将不会改变.当添加新文档到你的索引时候,它们被写入到新的段中.因此,在你的索引中,有很多激活的段.一次查询必须 ...

  7. Spring教程索引

    Spring教程索引 2016-11-15 1 入门 1 概述.深入浅出Spring(一)Spring概述 2 体系结构 3 环境设置 4 Hello World 实例 5 IoC 容器   IoC容 ...

  8. 【转】Mysql两种存储引擎的异同【MyISAM和InnoDB】

    MySQL默认采用的是MyISAM. MyISAM不支持事务,而InnoDB支持.InnoDB的AUTOCOMMIT默认是打开的,即每条SQL语句会默认被封装成一个事务,自动提交,这样会影响速度,所以 ...

  9. linux查找系统中占用磁盘空间最大的文件

    Q:下午有一客户磁盘空间占用很大,使用df查看磁盘剩余空间很小了,客户想知道是哪些文件占满了文件. Q1:在Linux下如何查看系统占用磁盘空间最大的文件? Q2:在Linux下如何让文件夹下的文件让 ...

  10. 【分区助手】如何扩大C盘容量?

    问题:C盘容量太小,想通过缩小其他盘(比如本例的F盘)来扩大C盘. 工具:分区助手 步骤: 1.下好分区助手后打开(该软件建议装在C盘),选择左侧的[扩大分区导向]. 2.选择下面那个,要先缩小F盘扩 ...