POJ 3414 Pots(BFS)
Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu
Description
You are given two pots, having the volume of A and B liters respectively. The following operations can be performed:
- 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).
Write a program to find the shortest possible sequence of these operations that will yield exactly C liters of water in one of the pots.
Input
On the first and only line are the numbers A, B, and C. These are all integers in the range from 1 to 100 and C≤max(A,B).
Output
The first line of the output must contain the length of the sequence of operations K. The following K lines must each describe one operation. If there are several sequences of minimal length, output any one of them. If the desired result can’t be achieved, the first and only line of the file must contain the word ‘impossible’.
Sample Input
3 5 4
Sample Output
6
FILL(2)
POUR(2,1)
DROP(1)
POUR(2,1)
FILL(2)
POUR(2,1) 怎么说呢,BFS,有六种状态
写起来还是有点烦的,要细心
#include<cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include<queue>;
using namespace std;
struct node
{
int av,aw,bv,bw;
int o[],pos;
}s;
bool vis[][];
int f;
node cur,next;
void fill(int i)
{
if(i==)
next.aw=next.av;
else if(i==)
next.bw=next.bv;
}
void drop(int i)
{
if(i==)
next.aw=;
else
next.bw=;
}
void pour(int a,int b)
{
if(a==&&b==){
if(next.aw>next.bv-next.bw)
{
next.aw-=(next.bv-next.bw);
next.bw=next.bv;
}
else
{
next.bw+=next.aw;
next.aw=;
}}
else
{
if(next.bw>next.av-next.aw)
{
next.bw-=(next.av-next.aw);
next.aw=next.av;
}
else
{
next.aw+=next.bw;
next.bw=;
}
}
}
void print(int i)
{
if(i==)
printf("POUR(1,2)\n");
else if(i==)
printf("POUR(2,1)\n");
else if(i==)
printf("FILL(1)\n");
else if(i==)
printf("FILL(2)\n");
else if(i==)
printf("DROP(1)\n");
else if(i==)
printf("DROP(2)\n");
}
bool bfs()
{
memset(vis,false,sizeof(vis));
s.aw=;s.bw=;
s.pos=;
vis[][]=true;
queue<node>Q;
Q.push(s);
while(!Q.empty())
{
cur=Q.front();
Q.pop();
if(f==cur.aw||f==cur.bw)
{
printf("%d\n",cur.pos);
for(int i=;i<cur.pos;i++)
{
print(cur.o[i]);
}
return true;
break;
}
//
if(cur.aw!=&&cur.bw!=cur.bv){
next=cur;
pour(,);
next.o[next.pos]=;
next.pos++;
if(!vis[next.aw][next.bw])
{
vis[next.aw][next.bw]=true;
Q.push(next);
}}
//
if(cur.bw!=&&cur.aw!=cur.av){
next=cur;
pour(,);
next.o[next.pos]=;
next.pos++;
if(!vis[next.aw][next.bw])
{
vis[next.aw][next.bw]=true;
Q.push(next);
}}
//
if(cur.aw!=cur.av){
next=cur;
fill();
next.o[next.pos]=;
next.pos++;
if(!vis[next.aw][next.bw])
{
vis[next.aw][next.bw]=true;
Q.push(next);
}}
//
if(cur.bw!=cur.bv){
next=cur;
fill();
next.o[next.pos]=;
next.pos++;
if(!vis[next.aw][next.bw])
{
vis[next.aw][next.bw]=true;
Q.push(next);
}}
//
if(cur.aw!=){
next=cur;
drop();
next.o[next.pos]=;
next.pos++;
if(!vis[next.aw][next.bw])
{
vis[next.aw][next.bw]=true;
Q.push(next);
}}
//
if(cur.bw!=){
next=cur;
drop();
next.o[next.pos]=;
next.pos++;
if(!vis[next.aw][next.bw])
{
vis[next.aw][next.bw]=true;
Q.push(next);
}}
}
return false;
}
int main()
{
while(scanf("%d%d%d",&s.av,&s.bv,&f)!=EOF)
{
if(!bfs())printf("impossible\n");
}
return ;
}
POJ 3414 Pots(BFS)的更多相关文章
- POJ 3414 Pots bfs打印方案
题目: http://poj.org/problem?id=3414 很好玩的一个题.关键是又16ms 1A了,没有debug的日子才是好日子.. #include <stdio.h> # ...
- poj 3414 Pots(bfs+输出路径)
Description You are given two pots, having the volume of A and B liters respectively. The following ...
- POJ - 3414 Pots BFS(著名倒水问题升级版)
Pots You are given two pots, having the volume of A and B liters respectively. The following operati ...
- POJ 3414 Pots (BFS/DFS)
Pots Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7783 Accepted: 3261 Special Ju ...
- poj 3414 Pots bfs+模拟
#include<iostream> #include<cstring> #define fillA 1 #define pourAB 2 #define dropA 3 #d ...
- POJ 3414 Pots ( BFS , 打印路径 )
题意: 给你两个空瓶子,只有三种操作 一.把一个瓶子灌满 二.把一个瓶子清空 三.把一个瓶子里面的水灌到另一个瓶子里面去(倒满之后要是还存在水那就依然在那个瓶子里面,或者被灌的瓶子有可能没满) 思路: ...
- poj 3414 Pots 【BFS+记录路径 】
//yy:昨天看着这题突然有点懵,不知道怎么记录路径,然后交给房教了,,,然后默默去写另一个bfs,想清楚思路后花了半小时写了120+行的代码然后出现奇葩的CE,看完FAQ改了之后又WA了.然后第一次 ...
- BFS POJ 3414 Pots
题目传送门 /* BFS:六种情况讨论一下,BFS轻松解决 起初我看有人用DFS,我写了一遍,TLE..还是用BFS,结果特判时出错,逗了好长时间 看别人的代码简直是受罪,还好自己终于发现自己代码的小 ...
- 广搜+输出路径 POJ 3414 Pots
POJ 3414 Pots Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13547 Accepted: 5718 ...
随机推荐
- [转]How to create an anonymous IDA PRO database (.IDB)
Source: http://www.0xebfe.net/blog/2013/01/13/how-to-create-an-anonymous-ida-pro-database-dot-idb/ P ...
- XDCTF 2013 code2 跳出死循环
题目:编写一个程序(比如kernel module),使附件2.c中的程序跳出死循环.2.c中的代码如下:#include int main(int argc, char *argv[]){int n ...
- Sqlserver 2008安装
##本文档所有安装操作都使用自动化脚本----###安装脚本的参数```#!setsaPassword=pass@word1 //sa数据库密码sqlComponent=SQLENGINE,REPLI ...
- 安装Win8
老毛桃安装Win8(哪里不会点哪里,so easy) 先来一张美女图,是不是很漂亮呢!继续往下看! 求推荐啊! 对于一个干IT的码农来说,会写代码不算什么,会装系统不算什么,及会写代码也会装系统的那才 ...
- 纯Python综合图像处理小工具(3)10种滤镜算法
<背景> 滤镜处理是图像处理中一种非常常见的方法.比如photoshop中的滤镜效果,除了自带的滤镜,还扩展了很多第三方的滤镜效果插件,可以对图像做丰富多样的变换:很多手机app实现了实 ...
- TFS体系结构和概念
TFS体系结构和概念 TFS是Team Fundation Server的简称,是微软VSTS的一部分,它是Microsoft应用程序生命周期管理(ALM)工具的核心协作平台,简单的说它是管理和开发软 ...
- Citrix 服务器虚拟化之四 Xenserver资源池
Citrix 服务器虚拟化之四 Xenserver资源池 台主机,尽管这种限制没有执行.池总是至少有一个物理节点,称为主.只有主节点公开管理界面(使用XenCenter和XenServer命令行界面 ...
- 怀念的东西:Pirka咖啡,芬兰的味道
怀念的东西:Pirka咖啡,芬兰的味道 前一段收到了小牛同学从芬兰托人寄来的咖啡.拿着提货单的时候,我满脑子问号.这寄货人是谁的呢,我完全没有印象.而且写的是食品.我又想起了最近报道的诈骗消息,给你寄 ...
- Android中使用开源框架Fresco处理图片
本文为原创博文,转载请注明原文链接:http://www.cnblogs.com/panhouye/p/6278116.html 关于Fresco的优点大家自行谷歌吧,它太强大太优秀了,我这一片小博文 ...
- Python3.5环境下安装wxPtyhon
Win7系统下,Python3.5环境下安装wxPtyhon, 已成功安装并运行. 1.先从下面网站下载对应的whl版本. https://wxpython.org/Phoenix/snapshot- ...