题目链接:http://poj.org/problem?id=3414

思路:bfs简单应用,增对瓶A或者瓶B进行分析就可以了,一共6种状态。

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std; struct Node{
int a,b,step;
char str[][];
}; int A,B,C;
bool mark[][];
bool bfs()
{
memset(mark,false,sizeof(mark));
queue<Node>que;
Node p,q;
p.a=,p.b=,p.step=;
que.push(p);
mark[][]=true;
while(!que.empty()){
p=que.front();
que.pop();
if(p.a==C||p.b==C){
printf("%d\n",p.step);
for(int i=;i<=p.step;i++){
printf("%s\n",p.str[i]);
}
return true;
}
if(p.a==){
q=p;
q.a=A;
q.step++;
strcpy(q.str[q.step],"FILL(1)");
if(!mark[q.a][q.b]){
mark[q.a][q.b]=true;
que.push(q);
}
}else if(p.a<=A){
q=p;
q.a=;
q.step++;
strcpy(q.str[q.step],"DROP(1)");
if(!mark[q.a][q.b]){
mark[q.a][q.b]=true;
que.push(q);
}
if(p.b<B){
q=p;
if(q.a+q.b<=B){
q.b+=q.a;
q.a=;
}else {
q.a=(q.b+q.a)-B;
q.b=B;
}
q.step++;
strcpy(q.str[q.step],"POUR(1,2)");
if(!mark[q.a][q.b]){
mark[q.a][q.b]=true;
que.push(q);
}
}
}
if(p.b==){
q=p;
q.b=B;
q.step++;
strcpy(q.str[q.step],"FILL(2)");
if(!mark[q.a][q.b]){
mark[q.a][q.b]=true;
que.push(q);
}
}else if(p.b<=B){
q=p;
q.b=;
q.step++;
strcpy(q.str[q.step],"DROP(2)");
if(!mark[q.a][q.b]){
mark[q.a][q.b]=true;
que.push(q);
}
if(p.a<A){
q=p;
if(q.b+q.a<=A){
q.a+=q.b;
q.b=;
}else {
q.b=(q.b+q.a)-A;
q.a=A;
}
q.step++;
strcpy(q.str[q.step],"POUR(2,1)");
if(!mark[q.a][q.b]){
mark[q.a][q.b]=true;
que.push(q);
}
}
}
}
return false;
} int main()
{
while(~scanf("%d%d%d",&A,&B,&C)){
if(!bfs())puts("impossible");
}
return ;
}

poj 3414(简单bfs)的更多相关文章

  1. Pots(POJ - 3414)【BFS 寻找最短路+路径输出】

    Pots(POJ - 3414) 题目链接 算法 BFS 1.这道题问的是给你两个体积分别为A和B的容器,你对它们有三种操作,一种是装满其中一个瓶子,另一种是把其中一个瓶子的水都倒掉,还有一种就是把其 ...

  2. POJ 3414 Pots bfs打印方案

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

  3. poj 3414 Pots bfs+模拟

    #include<iostream> #include<cstring> #define fillA 1 #define pourAB 2 #define dropA 3 #d ...

  4. POJ 1101 简单BFS+题意

    The Game 题意: Description One morning, you wake up and think: "I am such a good programmer. Why ...

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

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

  6. POJ 3414 Pots(BFS)

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

  7. poj 3278 简单BFS

    题意:给定农夫和奶牛的初始位置,农夫可以当前位置+1.-1.*2三种移动方式,问最少需要多少分钟抓住奶牛 AC代码: #include<cstdio> #include<cstrin ...

  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(著名倒水问题升级版)

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

随机推荐

  1. 站点搭建从零開始(五) WordPress的安装

    前面说了非常多废话.如今最终转到正题.WordPress的安装. 1.WordPress安装非常easy 假设你的server能通过应用中心一键安装WordPress,这一节就非常轻松了,基本上不须要 ...

  2. dmesg 时间转换脚本

    https://linuxaria.com/article/how-to-make-dmesg-timestamp-human-readable perl脚本 #!/usr/bin/perl use ...

  3. Oracle中查询某字段不为空或者为空的SQL语句怎么写

    比如 insert into table a (a1,b1)values("a1",''); 对于这种情况,因为表里存的是'',其实是没有内容的,要查询这个字段,不能直接使用 se ...

  4. Linux程序员福利 - 追女友神奇(Linux终端运行炫酷程序)

    概述 作为IT人员,给同事的感觉呆板,不会会浪漫,不懂情趣.其实不然,我们可以用我们的技能创造出IT人员独有的浪漫.girlLove脚本就可以实现IT人员的浪漫.girlLove本质上是一个简易的问答 ...

  5. PHP连接Azure Redis

    概述 Azure Redis缓存基于流行的开源Redis缓存,可以通过各种Redis客户端进行访问,这些客户端适用于许多编程语言.每个客户端有自身的API,用于通过Redis命令调用Redis缓存实例 ...

  6. javascript删除数组中的某个元素-----拓展Array 原型方法

    Array.prototype.remove = function (dx) { if(isNaN(dx) || dx > this.length) { return false; } var  ...

  7. MapReduce编程(七) 倒排索引构建

    一.倒排索引简单介绍 倒排索引(英语:Inverted index),也常被称为反向索引.置入档案或反向档案,是一种索引方法,被用来存储在全文搜索下某个单词在一个文档或者一组文档中的存储位置的映射. ...

  8. vivado sdk生成elf文件出错:make: Interrupt/Exception caught (code = 0xc00000fd, addr = 0x4227d3)

    vivado sdk生成elf文件出错:make: Interrupt/Exception caught (code = 0xc00000fd, addr = 0x4227d3) Might be a ...

  9. Haskell示例

    i :: Int i = --add, sub :: Int -> Int -> Int add, sub :: (Num a) => a -> a -> a add a ...

  10. 新标准C++程序设计读书笔记_运算符重载

    形式 返回值类型 operator 运算符(形参表) { …… } 运算符重载 (1)运算符重载的实质是函数重载(2)可以重载为普通函数,也可以重载为成员函数 class Complex { publ ...