题目: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. event事件:

    onabort: 图像的加载被中断onblur: 元素失去焦点onchange: 域的内容被改变onclick: 当用户点击某个对象时调用的事件句柄ondblclick: 当用户双击某个对象时调用的事 ...

  2. tomcat源码解读(1)–tomcat热部署实现原理

    tomcat的热部署实现原理:tomcat启动的时候会有启动一个线程每隔一段时间会去判断应用中加载的类是否发生变法(类总数的变化,类的修改),如果发生了变化就会把应用的启动的线程停止掉,清除引用,并且 ...

  3. scrapy-redis使用详解

    描述: 1.使用两台机器,一台是win10,一台是centos7,分别在两台机器上部署scrapy来进行分布式抓取一个网站 2.centos7的ip地址为192.168.1.112,用来作为redis ...

  4. 《C和指针》 读书笔记 -- 第9章 字符串、字符和字节

    1.字符串以NUL结尾,但字符串长度不包括NUl字节. 2.复制字符串 char *strcpy(char *dst,char const *src); 3.连接字符串 char *strcat(ch ...

  5. wampserver安装后的基本配置

    wampserver安装后的基本配置 1.WampServer的安装 下载好安装包后,你能在保存其文件夹中找到这样一个图标: 双击它,会弹出如下提示 提示信息:不要试图从Wamp5 1.x(x代表任意 ...

  6. 查看某一个点是否在某个多边形内 使用ST_Contains函数

    查看某一个点是否在某个多边形内  使用ST_Contains函数 --LINESTRING ( 121.312350 30.971457 , 121.156783 31.092221 , 121.35 ...

  7. Unity3d 如何找到游戏对象并改变其颜色

    //游戏对象 private var obj:GameObject; //渲染器 private var render:Renderer; //贴图 private var texture:Textu ...

  8. 【BZOJ 2301】[HAOI2011]Problem b

    Description 对于给出的n个询问,每次求有多少个数对(x,y),满足a≤x≤b,c≤y≤d,且gcd(x,y) = k,gcd(x,y)函数为x和y的最大公约数. Input 第一行一个整数 ...

  9. 3.8 spring - AbstractBeanDefinition 介绍

    之前,我们已尽完成了xml 文档到 GenericBeanDefinition的转换, 也就是说,到这里,所有的配置都可以在GenericBeanDefinition 的实例中找到对应的配置. Gen ...

  10. Comet、SSE、Web Socket

    来自<javascript高级程序设计 第三版:作者Nicholas C. Zakas>的学习笔记(十一) Comet Comet是一种更加高级的Ajax技术("服务器推送&qu ...