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 ...
随机推荐
- Pi
Math]Pi 数学知识忘地太快,在博客记录一下pi的生成. 100 Decimal places 3.1415926535897932384626433832795028841971693993 ...
- NoSQL发展简史、粗略分类及选择
这里对近来看的部分NoSQL资料做一个汇总记录,主要包括简史.粗略分类及数据库选择的考虑事项.NoSQL常见的解释是“non-relational”,有时也称作Not Only SQL. 1. ...
- YPreLoad
Javascript库 发布我的控件系列:图片预加载控件YPreLoad v1.0 摘要: 介绍大家好!很高兴向大家介绍我的图片预加载控件YPreLoad.它可以帮助您预加载图片,并且能显示加载的 ...
- C# 毛玻璃效果
- Mahout之(三)相似性度量
User CF 和 Item CF 都依赖于相似度的计算,因为只有通过衡量用户之间或物品之间的相似度,才能找到用户的“邻居”,才能完成推荐.上文简单的介绍了相似性的计算,但不完全,下面就对常用的相似度 ...
- 让MyEclipse里的Tomcat自动reloadable
1 修改server.xml Context path="/***" docBase="XXX" reloadable="true"/&g ...
- 目标HttpController在ASP.NET Web API中是如何被激活的:目标HttpController的创建
目标HttpController在ASP.NET Web API中是如何被激活的:目标HttpController的创建 通过上面的介绍我们知道利用HttpControllerSelector可以根据 ...
- ArcEngine关于单位转换示例
示例界面: 转换代码: private void Button1_Click(object sender, System.Windows.RoutedEventArgs e) { // Get the ...
- NHibernate总结
NHibernate总结 现在的项目中数据访问使用的是NHibernate的一个ORM框架,小弟也是在后期加入项目组,之前对NHibernate就一直没有接触过,所以一直在学习NHibernate,都 ...
- 长轮询实现Chat并迁移到Azure测试
长轮询实现Chat并迁移到Azure测试 公司的OA从零开始进行开发,继简单的单点登陆.角色与权限.消息中间件之后,轮到在线即时通信的模块需要我独立去完成.这三周除了逛网店见爱*看动漫接兼职,基本上都 ...