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 ...
随机推荐
- iOS制作Static Library(静态库),实现多工程的连编
在iOS开发中,我们会发现一些偏底层或基础代码是直接可以复用的,当我们换一个项目,改变的只需要是偏上层的业务逻辑代码,所以我们可以把这部分基础代码制作为一个静态库static library,并不断扩 ...
- python学习之路三(文件读写)
# -*- coding: utf-8 -* ''' Created on 2013-7-29 @author: lixingle ''' import os #引入操作文件和目录的函数包 impor ...
- javascript闭包1
javascript闭包 在学习javascript闭包之前,需要先了解一下"作用域链". 每一段javascript代码都有一个与之关联的作用域链(scope chain),这个 ...
- Ruby的对象模型
目录 备注对象模型无图无真相基本规则代码示例如何修改Singleton Class?如何修改类型,如Child?类型方法是特殊的实例方法,这些方法定义在类型的Singleton Class中.备注 备 ...
- 对比AutoResetEvent和ManualResetEvent
ManualResetEvent和AutoResetEvent 比较 ManualResetEvent和AutoResetEvent都继承自EventWaitHandler,它们的唯一区别就在于父类 ...
- php三中页面跳转方式(header、location、refresh) 乐杨俊
反法三:也是用的比较多的
- Epicor系统二次开发
Epicor系统二次开发 一.获取或修改界面EpiDataView的字段数据(Get EpiDataView data) C# EpiDataView edv = (EpiDataView)oTran ...
- C#通过外部别名,解决DLL冲突问题
今天遇到一个有两个DLL文件,命名空间,部分类名与部分方法名一样,但是方法的功能实现不一样.调用方法时,无法调用指定DLL的指定方法.在网上找了好多,简单总结一下. 1.首先添加引用,不细说. 2.右 ...
- [转载]关于shell脚本的基本语法
关于shell脚本的基本语法 整理于:2014-03-31,何俭飞,mymladdr@sina.com 一.执行 1.shell脚本如果要被执行,一般地必须要有执行权限"x"(除了 ...
- USACO 3.4 Electric Fence
Electric FenceDon Piele In this problem, `lattice points' in the plane are points with integer coord ...