#include<iostream>
#include<cstring>
#define fillA 1
#define pourAB 2
#define dropA 3
#define fillB 4
#define pourBA 5
#define dropB 6 #define N 10000 using namespace std;
int vis[][];
class node
{
public:
int A, B;
int dir;
node()
{
A=;
B=;
}
}; int a, b, c, tot;
node q[N];
int pre[N]; void printPath(int index)
{
if(index==)
{
cout<<tot<<endl;
return ;
}
++tot;
printPath(pre[index]);
switch(q[index].dir)
{
case :
cout<<"FILL(1)";
break;
case :
cout<<"POUR(1,2)";
break;
case :
cout<<"DROP(1)";
break;
case :
cout<<"FILL(2)";
break;
case :
cout<<"POUR(2,1)";
break;
case :
cout<<"DROP(2)";
break;
}
cout<<endl;
} int bfs()
{
int head=, rear=, r;
node cur, next;
memset(vis, , sizeof(vis));
pre[]=;
vis[][]=;
while(head<rear)
{
cur=q[head];
if(cur.A==c || cur.B==c)
{
printPath(head);
return ;
}
next=cur;
if(!vis[a][cur.B])//将 A 装满
{
next.A=a;
next.dir=fillA;
pre[rear]=head;
vis[a][cur.B]=;
q[rear++]=next;
}
if(!vis[][cur.B])//将 A 倒掉
{
next.A=;
next.dir=dropA;
pre[rear]=head;
vis[][cur.B]=;
q[rear++]=next;
}
r=b-cur.B;
next.dir=pourAB;
if(r>cur.A && !vis[][cur.B+cur.A])//将A 倒向 B
{
next.A=;
next.B=cur.B+cur.A;
pre[rear]=head;
q[rear++]=next;
vis[][cur.B+cur.A]=;
}
else if(r<=cur.A && !vis[cur.A-r][cur.B+r])
{
next.A=cur.A-r;
next.B=cur.B+r;
pre[rear]=head;
q[rear++]=next;
vis[cur.A-r][cur.B+r]=;
} next=cur;//开始错在这里了, 忘记了从新给next赋值了
if(!vis[cur.A][b])//将 B 装满
{
next.B=b;
next.dir=fillB;
pre[rear]=head;
q[rear++]=next;
vis[cur.A][b]=;
}
if(!vis[cur.A][])//将 B 倒掉
{
next.B=;
next.dir=dropB;
pre[rear]=head;
q[rear++]=next;
vis[cur.A][]=;
}
r=a-cur.A;
next.dir=pourBA;
if(r>cur.B && !vis[cur.B+cur.A][])//将B 倒向 A
{
next.B=;
next.A=cur.B+cur.A;
pre[rear]=head;
q[rear++]=next;
vis[cur.B+cur.A][]=;
}
else if(r<=cur.B && !vis[cur.A+r][cur.B-r])
{
next.A=cur.A+r;
next.B=cur.B-r;
pre[rear]=head;
q[rear++]=next;
vis[cur.A+r][cur.B-r]=;
}
++head;
}
return ;
} int main()
{
while(cin>>a>>b>>c)
{
tot=;
if(c>a && c>b)
{
cout<<"impossible"<<endl;
continue;
}
if(!bfs())
cout<<"impossible"<<endl;
}
return ;
}

poj 3414 Pots bfs+模拟的更多相关文章

  1. POJ 3414 Pots bfs打印方案

    题目: http://poj.org/problem?id=3414 很好玩的一个题.关键是又16ms 1A了,没有debug的日子才是好日子.. #include <stdio.h> # ...

  2. POJ 3414 Pots(BFS)

    Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Description You are g ...

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

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

  4. POJ - 3414 Pots BFS(著名倒水问题升级版)

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

  5. POJ 3414 Pots (BFS/DFS)

    Pots Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7783   Accepted: 3261   Special Ju ...

  6. POJ 3414 Pots ( BFS , 打印路径 )

    题意: 给你两个空瓶子,只有三种操作 一.把一个瓶子灌满 二.把一个瓶子清空 三.把一个瓶子里面的水灌到另一个瓶子里面去(倒满之后要是还存在水那就依然在那个瓶子里面,或者被灌的瓶子有可能没满) 思路: ...

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

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

  8. BFS POJ 3414 Pots

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

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

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

随机推荐

  1. js排序算法总结—冒泡,快速,选择,插入,希尔,归并

    相信排序是任何一个程序猿都会用到的东西,今天简单总结记录下常见的排序算法. 一.冒泡排序 说起冒泡排序,可能每个人都不会陌生,实现思路相当简单明了,就是不停的对数组进行两两比较,将较大(较小)的一项放 ...

  2. MAC帧和IP包的分析

    ping了12次岭南师范学院官网后退出   抓到的包如下   各个名词解释

  3. jQuery中设置form表单中action的值的方法

    下面介绍在jQuery中设置form表单中action的值的方法. $("#myFormId").attr("action", "userinfo.s ...

  4. html5shiv.js and respond.min.js

    因为用到这两个插件,所以记录下来.. html5shiv:解决ie9以下浏览器对html5新增标签的不识别,并导致CSS不起作用的问题. respond.min:让不支持Css3 Media Quer ...

  5. Hibernate4 实例

    一.hibernate需要的配置文件 首先hibernate中有两种xml文件. .cfg,xml文件负责配置连接数据库的信息.指定映射类.指定hbm映射文件. .hbm.xml文件负责配置持久化类和 ...

  6. hibernate不同版本获取获取sessionFactory

    hibernate4时,我们采用以下方式获取会话工厂: // 1. 解析我们在hibernate.cfg.xml中的配置 Configuration configuration = new Confi ...

  7. What's the difference between a stub and mock?

    I believe the biggest distinction is that a stub you have already written with predetermined behavio ...

  8. return array 评论添加状态和提示信息

    ThinkSNS漏洞系列第一弹,某处处理不当导致SQL注入 漏洞点出现在Comment Widget里:\addons\widget\CommentWidget\CommentWidget.class ...

  9. 远程桌面连接 win7 主机提示“您的凭据不工作”的解决办法

    搞了大半天,找了百度N中方式操作,至少翻看10种以上解决方式,结果还是不得行 索性使用了360搜索,搜了几次就搞定了. 解决办法: “ 最重要一点, 主机上要允许用户以非guest身份登录:主机上运行 ...

  10. 数据库中数据DELETE了怎样恢复

    USE [BBDAS_FP_DATA] GO /****** Object: StoredProcedure [dbo].[Recover_Deleted_Data_Proc] Script Date ...