poj 3414 Pots ( bfs )
题目: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 )的更多相关文章
- POJ 3414 Pots(罐子)
POJ 3414 Pots(罐子) Time Limit: 1000MS Memory Limit: 65536K Description - 题目描述 You are given two po ...
- poj 3414 Pots (bfs+线索)
Pots Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10071 Accepted: 4237 Special J ...
- POJ 3414 Pots(BFS+回溯)
Pots Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11705 Accepted: 4956 Special J ...
- poj 3414 Pots 【BFS+记录路径 】
//yy:昨天看着这题突然有点懵,不知道怎么记录路径,然后交给房教了,,,然后默默去写另一个bfs,想清楚思路后花了半小时写了120+行的代码然后出现奇葩的CE,看完FAQ改了之后又WA了.然后第一次 ...
- poj 3414 Pots【bfs+回溯路径 正向输出】
题目地址:http://poj.org/problem?id=3414 Pots Time Limit: 1000MS Memory Limit: 65536K Total Submissions ...
- poj 3414 Pots(广搜BFS+路径输出)
转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents 题目链接:id=3414">http://poj.org/probl ...
- 【POJ - 3414】Pots(bfs)
Pots 直接上中文 Descriptions: 给你两个容器,分别能装下A升水和B升水,并且可以进行以下操作 FILL(i) 将第i个容器从水龙头里装满(1 ≤ i ≤ 2); DRO ...
- POJ 3414 Pots (dfs,这个代码好长啊QAQ)
Description You are given two pots, having the volume of A and B liters respectively. The following ...
- POJ 3414 pots (未解决)
http://poj.org/problem?id=3414 #include <iostream> #include <cstdio> #include <queue& ...
随机推荐
- mongodb 级联操作查询时,关联条件
ObjectId id=new ObjectId("123"); c=c.where("relation_type.$id").is(id);
- 微信公众号-开发者-自定义菜单-CLICK事件处理
想点击菜单,推送消息.功能很简单,坑了我一天时间.在此笔记. 菜单代码: { "button":[ { "type":"click", &q ...
- TCP三次握手原理与SYN攻击
本文内容包括以下几点 1.TCP三次握手四次挥手解析 2.迭代型服务器程序编写,并给出客户端,结合这一模式详细介绍Berkeley套接字的使用 3.介绍SYN攻击的原理 TCP连接建立,传输数据,连接 ...
- php 批量替换html标签的实例代码
php批量替换html标签的实例代码分享. 1.把html元素全部去掉,或者保留某几个html标签 <?php $text = '<p>Test paragraph.</p ...
- thinkphp 使用过程中遇到的一个小函数
1.实现导出Excel文件,并在导出的文件中显示图片 //导出 public function push(){ $goods_list=M('Dajia')->select(); $data = ...
- 分享自lordinloft 《[转载]COMPILE_OPT 的用法介绍》
来源:http://blog.sina.com.cn/s/blog_63180b75010117oj.html#bsh-73-372143085
- ARCGIS 10中添加excel点字段生产点shp文件的工具
菜单栏中——文件——添加数据——添加XY数据——选择excel。 选择x,y和投影(注意这里投影应该是原数据的本身投影,不能直接选择你要转换的投影)完成。
- 关于对db_block_gets的理解与实验
实验 一. 自己手动创建的小表 创建一个区大小为 40k SYS@ORCL>show parameter db_block_size NAME ...
- iOS 通览(一) - 技巧、特性及规范
一.编译器特性 1.ARC. ARC是编译器特性.项目中使用了ARC,编译器会在项目编译的时候自动添加OC对象计数器release代码.并且使用了ARC,项目中将不允许出现release.retain ...
- objective-C 中两种实现动画的方法(转)
转发自:http://wayne173.iteye.com/blog/1250232 第一种方法: [UIView beginAnimations:@"Curl"context: ...