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 ≤ 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
    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 A, B, 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)

题意:有两个空瓶 a,b是它们的容量,c是容量目标。 能够有三种操作 充满随意一瓶。倒空随意一瓶,将随意一瓶倒入还有一瓶(能剩下但不能溢出);求随意一瓶的体积
达到目标体积所须要的最小操作数。并依此输出该操作。

代码:
#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
using namespace std;
int m,n,k;
int vis[105][105];
char opr[20][20]= {" " , "FILL(1)" , "FILL(2)" , "DROP(1)" , "DROP(2)" , "POUR(1,2)" , "POUR(2,1)" }; //共6种操作
struct node
{
int x,y,step;
int w[200]; //用来记录路径的数组数组
};
void bfs( )
{
int i,j;
int kx,ky;
memset(vis,0,sizeof(vis));
queue<node>q;
node now,next;
now.x=0,now.y=0,now.step=0;
q.push(now);
vis[0][0]=1;
while(!q.empty())
{
now=q.front();
q.pop();
if(now.x==k||now.y==k)
{
cout<<now.step<<endl;
for( i=1; i<=now.step; i++)
{
cout<<opr[now.w[i]]<<endl;
}
return;
}
for(i=0; i<6; i++) // 共六种操作
{
if(i==0)
{
next.y=now.y;
next.x=m;
now.w[now.step+1]=1; //随时更新
}
else if(i==1)
{
next.x=now.x;
next.y=n;
now.w[now.step+1]=2;
}
else if(i==2)
{
next.y=now.y;
next.x=0;
now.w[now.step+1]=3;
}
else if(i==3)
{
next.x=now.x;
next.y=0;
now.w[now.step+1]=4;
}
else if(i==4)
{
if(n-now.y>now.x) //每种pour 应有两种情况
{
next.x=0;
next.y=now.x+now.y;
}
else
{
next.y=n;
next.x=now.x-n+now.y;
}
now.w[now.step+1]=5;
}
else if(i==5)
{
if(m-now.x>now.y)
{
next.y=0;
next.x=now.x+now.y;
}
else
{
next.x=m;
next.y=now.y-m+now.x;
}
now.w[now.step+1]=6;
}
if(vis[next.x][next.y]==1)
continue;
vis[next.x][next.y]=1;
next.step=now.step+1;
for(j=1; j<=next.step; j++) //记录之前的行动
{
next.w[j]=now.w[j];
}
q.push(next);
}
}
cout<<"impossible"<<endl; //不要忘了这个情况
return;
}
int main()
{
int i;
while(cin>>m>>n>>k)
{
bfs();
}
return 0;
}

通过自己对队列的理解成功写出了代码 还是挺开心的。

。。

POJ 3414 Pots 记录路径的广搜的更多相关文章

  1. POJ 3984 迷宫问题 记录路径的广搜

    主要是学一下如何在广搜中记录路径:每找到一个点我就记录下这个点是由那个点得来的,这样我找到最后个点后,我就可以通过回溯得到我走过的路径,具体看代码吧~ #include<cstdio> # ...

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

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

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

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

  4. POJ 3414 Pots(罐子)

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

  5. BFS POJ 3414 Pots

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

  6. (poj)3414 Pots (输出路径的广搜)

    Description You are given two pots, having the volume of A and B liters respectively. The following ...

  7. poj 3414 Pots(广搜BFS+路径输出)

    转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents 题目链接:id=3414">http://poj.org/probl ...

  8. (简单) POJ 3414 Pots,BFS+记录路径。

    Description You are given two pots, having the volume of A and B liters respectively. The following ...

  9. poj 3414 Pots(bfs+输出路径)

    Description You are given two pots, having the volume of A and B liters respectively. The following ...

随机推荐

  1. IOS系统对fixed定位支持不好的解决方法

    问题: IOS 中所有浏览器,当页面上的输入框获得焦点时,呼出键盘. 页面底部的导航栏(position:fixed)会被键盘顶到页面的中间. 而当输入框失去焦点时,导航栏停留在页面中间,造成页面错乱 ...

  2. FMXUI - UI.Dialog 示例

    在 FMXUI 开源库,增加了 UI.Dialog 单元.此单元实现了跨平台的基础对话框组件.使用时引用 UI.Dialog 即可.如果需要自定义对话框的样式, 可以添加一个 TDialogStyle ...

  3. Shell脚本笔记

      如何查询文件里的某个字符串? grep “字符串” 文件 例:grep "abc" tmp.txt   如何将查询出来的内容赋给变量? str=$(grep "abc ...

  4. Cppcheck 1.54 C/C++静态代码分析工具

    Cppcheck是一个C/C++代码分析工具,只检测那些编译器通常无法检测到的bug类型.   官方上建议让编译器提供尽量多的警告提示:1.使用Visual C++的话,应使用警告等级4 2.使用GC ...

  5. Nodejs 项目开发

    最近这几个月都在学习nodejs. 国内nodejs的资料相对较少,就我所搜索到的,CSDN目前的代码托管平台有不少从github弄过来的开源镜像,其它的不错的社区有cnodejs,byvoid的个人 ...

  6. Redis bio

    还是一个很小的模块. bio就是background io的意思,既然要background,就要创建线程,创建几个线程呢?有几种类型的io,就创建几个线程.同一种类型的job需要排队,所以存放各自的 ...

  7. ibatis通过Map封装参数调用存储过程

    一.存储过程如下(领导写的) CREATE OR REPLACE PROCEDURE agent_UIMOrIMEICheck_pro ( I_CARD_NO IN VARCHAR2, --UIM卡或 ...

  8. Qt之多线程

    源地址:http://blog.csdn.net/liuhongwei123888/article/details/6072320 Qt 是一种基于 C++ 的跨平台 GUI 系统,能够提供给用户构造 ...

  9. Javascript跨域请求的几种解决方法

    什么情况下才会出现跨域? 假设域名是:http://www.example.com.cn/ 如果所请求的域名跟这个域名不致,这种情况就是跨域,由于跨域存在漏洞,所以一般来说正常的跨域请求方式是请求不到 ...

  10. 译文:前端性能的重要性 The Importance of Frontend Performance

    欢迎訪问我的主页.最新的文章我会首先公布在个人主页上: http://blog.guaidm.com/shocky/ 原书下载地址:http://pan.baidu.com/s/1pJocRwB 在我 ...