题目: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. Delphi摄像头操作

    /*Title:Delphi摄像头操作 *Author:Insun *Blog:http://yxmhero1989.blog.163.com *From:www.4safer.com */ 为了笔耕 ...

  2. 使用rar打包多个文件为exe可执行文件

    需求分析:有些机友在刷recovery的时候不知道如何刷入,于是产生写bat脚本和打包为exe可执行文件,只要机友正确安装好驱动后连接手机双击就可以刷入rec了 解决过程: 需要打包的文件 操作过程截 ...

  3. c语言背后的运行机制

    目的:通过分析c语言转换成汇编代码后的执行过程对汇编语言和X86构架有一个初步认识 实验代码 #include <stdio.h> int g(int x) { ; } int f(int ...

  4. SQL 跨服务器数据库增、删、改、查(二)

    --创建链接服务器 exec sp_addlinkedserver 'jx3xxiednr3ucidf', ' ', 'SQLOLEDB', 'jx3xxiednr3ucidf' exec sp_ad ...

  5. ASP.NET如何获取根目录的方法汇总

    编写程序的时候,经常需要用的项目根目录,自己总结如下: 1.取得控制台应用程序的根目录方法 方法1.Environment.CurrentDirectory 取得或设置当前工作目录的完整限定路径 方法 ...

  6. Code for the Homework2

    第二次作业,最近有点忙,一直没写,先发一下,关节角计算有点问题,后面抽时间改 #include<iostream> #include <Eigen/Dense> #includ ...

  7. mysql 获取季度的第一天 本月的第一天,本周的第一天sql语句(转)

    感谢:http://www.111cn.net/database/110/d45124323da8d2d87b80f78319987eda.htm 查看同主题的另一篇博客:http://blog.cs ...

  8. linux命令useradd添加用户详解

    1.作用 useradd或adduser命令用来建立用户帐号和创建用户的起始目录,使用权限是超级用户. 2.格式 useradd [-d home] [-s shell] [-c comment] [ ...

  9. hdu 1176

    简单DP  类似于在一个矩形中求最长路径 /************************************************************************* > ...

  10. spoj 2178

    好水...... #include<cstdio> #include<cstdlib> #include<cstring> #include<algorith ...