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

题意:给出了两个瓶子的容量A,B, 以及一个目标水量C,

对A、B可以有如下操作:

FILL(i)        fill the pot i (1 ≤ i ≤ 2) from the tap;

DROP(i)      empty the pot i to the drain;

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).

问经过哪几个操作后能使得任意一个瓶子的残余水量为C。

若不可能得到则输出impossible

 #include <iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<stack>
#include<queue>
#include<iomanip>
#include<cmath>
#include<map>
#include<vector>
#include<algorithm>
using namespace std; int vis[][];
int a,b,c;
struct node
{
int x,y,step;
}pos,next; struct way
{
int x,y,f;
}before[][]; void pri(int x,int y)
{
stack<int>st;
while(x!=||y!=)
{
st.push(before[x][y].f);
int tx=before[x][y].x;
int ty=before[x][y].y;
x=tx; y=ty;
}
while(!st.empty())
{
switch(st.top())
{
case :printf("DROP(1)\n"); break;
case :printf("DROP(2)\n"); break;
case :printf("FILL(1)\n"); break;
case :printf("FILL(2)\n"); break;
case :printf("POUR(1,2)\n"); break;
case :printf("POUR(2,1)\n"); break;
}
st.pop();
}
}
int bfs()
{
queue<node>q;
next.x=; next.y=; next.step=;
vis[][]=;
q.push(next);
while(!q.empty())
{
pos=q.front();
//cout<<pos.x<<" "<<pos.y<<" "<<pos.step<<" ";
//cout<<before[pos.x][pos.y].x<<" "<<before[pos.x][pos.y].y<<" "<<before[pos.x][pos.y].f<<endl;
q.pop();
if(pos.x==c||pos.y==c)
{
cout<<pos.step<<endl;
pri(pos.x,pos.y);
return ;
}
if(!vis[][pos.y]&&pos.x!=)
{
next.x=; next.y=pos.y; next.step=pos.step+;
q.push(next);
vis[][pos.y]=;
before[][pos.y]=(struct way){pos.x,pos.y,};
}
if(!vis[pos.x][]&&pos.y!=)
{
next.x=pos.x; next.y=; next.step=pos.step+;
q.push(next);
vis[pos.x][]=;
before[pos.x][]=(struct way){pos.x,pos.y,};
}
if(!vis[a][pos.y]&&pos.x!=a)
{
next.x=a; next.y=pos.y; next.step=pos.step+;
q.push(next);
vis[a][pos.y]=;
before[a][pos.y]=(struct way){pos.x,pos.y,};
}
if(!vis[pos.x][b]&&pos.y!=b)
{
next.x=pos.x; next.y=b; next.step=pos.step+;
q.push(next);
vis[pos.x][b]=;
before[pos.x][b]=(struct way){pos.x,pos.y,};
}
if(pos.x>&&pos.y<b)
{
int t=min(pos.x,b-pos.y);
if(!vis[pos.x-t][pos.y+t])
{
q.push((struct node){pos.x-t,pos.y+t,pos.step+});
vis[pos.x-t][pos.y+t]=;
before[pos.x-t][pos.y+t]=(struct way){pos.x,pos.y,};
}
}
if(pos.x<a&&pos.y>)
{
int t=min(pos.y,a-pos.x);
if(!vis[pos.x+t][pos.y-t])
{
q.push((struct node){pos.x+t,pos.y-t,pos.step+});
vis[pos.x+t][pos.y-t]=;
before[pos.x+t][pos.y-t]=(struct way){pos.x,pos.y,};
}
}
}
return ;
}
int main()
{
cin>>a>>b>>c;
memset(vis,,sizeof(vis));
if(bfs()==)
cout<<"impossible"<<endl;
return ;
}

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

  1. POJ 3414 Pots(罐子)

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

  2. poj 3414 Pots (bfs+线索)

    Pots Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10071   Accepted: 4237   Special J ...

  3. POJ 3414 Pots(BFS+回溯)

    Pots Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11705   Accepted: 4956   Special J ...

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

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

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

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

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

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

  7. 【POJ - 3414】Pots(bfs)

    Pots 直接上中文 Descriptions: 给你两个容器,分别能装下A升水和B升水,并且可以进行以下操作 FILL(i)        将第i个容器从水龙头里装满(1 ≤ i ≤ 2); DRO ...

  8. POJ 3414 Pots (dfs,这个代码好长啊QAQ)

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

  9. POJ 3414 pots (未解决)

    http://poj.org/problem?id=3414 #include <iostream> #include <cstdio> #include <queue& ...

随机推荐

  1. (转载)直接用SQL语句把DBF导入SQLServer

    告诉大家一个直接用SQL语句把DBF导入SQLServer,以及txt导入Access的方法,大家抛弃BatchMove吧来自:碧血剑告诉你一个最快的方法,用SQLServer连接DBF在SQLSer ...

  2. python 对象持久化 pickle模块

    用到python 序列化 比如我们可以把一些配置的信息放到数组,字典或者做为类的属性,然后对数据进行 序列化,再把序列化好的数据放到文件里或者直接放到数据库里,这样可以方便下次要用数据的时候 对数据进 ...

  3. Linux学习系列之Linux入门(一)linux安装与入门

    第一篇:安装并配置Linux开发环境 一.安装linux: 主要安装Linux的发行版,到目前为之,主要的发行版有: 比较常用的是Ubuntu.redhat和centOS,主要的安装方法详细: Ubu ...

  4. 1054. The Dominant Color (20)

    时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Behind the scenes in the compute ...

  5. easy ui 表单元素input控件后面加说明(红色)

    <%-- 上传图片到图库基本信息且将图片关联到图集 开始--%> <div id="win_AddPicLib" class="easyui-windo ...

  6. python学习笔记11(函数二): 参数的传递、变量的作用域

    一.函数形参和实参的区别 形参全称是形式参数,在用def关键字定义函数时函数名后面括号里的变量称作为形式参数. 实参全称为实际参数,在调用函数时提供的值或者变量称作为实际参数. >>> ...

  7. 3.3 spring-meta子元素的使用与解析

    1. meta元素的使用 在解析元数据的分析之前,我们先回顾一下 meta属性的使用: <bean id="car" class="test.CarFactoryB ...

  8. [转载]MongoDB设置访问权限、设置用户

    MongoDB已经使用很长一段时间了,基于MongoDB的数据存储也一直没有使用到权限访问(MongoDB默认设置为无权限访问限制),今天特地花了一点时间研究了一下,研究成果如下: 注:研究成果基于W ...

  9. MVC4中Ajax.BeginForm OnSuccess 不执行以及控制器返回JsonResult 提示下载的原因

    这几天学习MVC的过程中,在学习Ajax.BeginForm时,一直遇到2个问题: 一. Ajax.BeginForm OnSuccess事件不执行 二.提交表单后,浏览器不识别json字符串,提示下 ...

  10. zoj 2387

    额  一个贪心  好难想到 ...... #include <cstring> #include <cstdio> #include <algorithm> #in ...