(简单) POJ 3414 Pots,BFS+记录路径。
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.
就是对两个杯子进行操作了,装满,倒空,倒过去。两个水杯里的水表示一个状态,然后BFS就好,要记录路径的话,记住每一个的父亲,然后最后反着来一遍就好了。
代码如下:
#include<iostream>
#include<cstring>
#include<cmath>
#include<queue> using namespace std; int A,B,C;
int rem[][];
int shorem[]; struct state
{
int a,b;
int type;
int faa,fab;
int num; state() {}
}; state sta[][]; void showans(state *e)
{
int cou=; while(e->a||e->b)
{
shorem[cou++]=e->type; e=&sta[e->faa][e->fab];
} cout<<cou<<endl;
for(int i=cou-;i>=;--i)
switch(shorem[i])
{
case :
cout<<"FILL(1)\n";
break;
case :
cout<<"FILL(2)\n";
break;
case :
cout<<"DROP(1)\n";
break;
case :
cout<<"DROP(2)\n";
break;
case :
cout<<"POUR(1,2)\n";
break;
case :
cout<<"POUR(2,1)\n";
break;
} } void slove()
{
queue <state *> que;
state *temp;
int t1,t2,t3; sta[][].faa=sta[][].fab=-;
sta[][].type=;
sta[][].num=;
que.push(&sta[][]); while(!que.empty())
{
temp=que.front();
que.pop(); if(temp->a==C||temp->b==C)
{
showans(temp);
return;
} t1=temp->a;
t2=temp->b; if(sta[A][t2].num==-)
{
sta[A][t2].num=temp->num+;
sta[A][t2].faa=t1;
sta[A][t2].fab=t2;
sta[A][t2].type=; que.push(&sta[A][t2]);
}
if(sta[t1][B].num==-)
{
sta[t1][B].num=temp->num+;
sta[t1][B].faa=t1;
sta[t1][B].fab=t2;
sta[t1][B].type=; que.push(&sta[t1][B]);
}
if(sta[][t2].num==-)
{
sta[][t2].num=temp->num+;
sta[][t2].faa=t1;
sta[][t2].fab=t2;
sta[][t2].type=; que.push(&sta[][t2]);
}
if(sta[t1][].num==-)
{
sta[t1][].num=temp->num+;
sta[t1][].faa=t1;
sta[t1][].fab=t2;
sta[t1][].type=; que.push(&sta[t1][]);
}
t3=min(t1,B-t2);
if(sta[t1-t3][t2+t3].num==-)
{
sta[t1-t3][t2+t3].num=temp->num+;
sta[t1-t3][t2+t3].faa=t1;
sta[t1-t3][t2+t3].fab=t2;
sta[t1-t3][t2+t3].type=; que.push(&sta[t1-t3][t2+t3]);
}
t3=min(t2,A-t1);
if(sta[t1+t3][t2-t3].num==-)
{
sta[t1+t3][t2-t3].num=temp->num+;
sta[t1+t3][t2-t3].faa=t1;
sta[t1+t3][t2-t3].fab=t2;
sta[t1+t3][t2-t3].type=; que.push(&sta[t1+t3][t2-t3]);
}
} cout<<"impossible\n";
} int main()
{
ios::sync_with_stdio(false); for(int i=;i<=;++i)
for(int j=;j<=;++j)
{
sta[i][j].a=i;
sta[i][j].b=j;
} while(cin>>A>>B>>C)
{
for(int i=;i<=;++i)
for(int j=;j<=;++j)
sta[i][j].num=-; slove();
} return ;
}
(简单) POJ 3414 Pots,BFS+记录路径。的更多相关文章
- 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 POJ - 3414 (搜索+记录路径)
Pots Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 22688 Accepted: 9626 Special J ...
- POJ 3414 Pots bfs打印方案
题目: http://poj.org/problem?id=3414 很好玩的一个题.关键是又16ms 1A了,没有debug的日子才是好日子.. #include <stdio.h> # ...
- 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)
Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu Description You are g ...
- 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+记录路径 】
//yy:昨天看着这题突然有点懵,不知道怎么记录路径,然后交给房教了,,,然后默默去写另一个bfs,想清楚思路后花了半小时写了120+行的代码然后出现奇葩的CE,看完FAQ改了之后又WA了.然后第一次 ...
随机推荐
- MyBaits 错误分析
错误原因:在DAO的映射文件中,在映射标签中的type类型写成DAO类了,应该写成javaBean
- PullToRefreshListView上拉加载、下拉刷新 eclipse项目
说明:此项目实在fragment中的,需要依赖library完成,还用到了Xuitls.jar包.使用了Pull解析XML eclipse中的项目: //注意:此刷新功能是使用的第三方的PullToR ...
- vbs 截图
'VBS截屏.vbs ' Win7x64 测试通过(已安装Word2007): '参考: ' http://qtp.blogspot.com/2010/02/screenshot-vbscript ...
- location对象的属性和方法应用(解析URL)
本文将与大家分享下location对象使用属性和方法来解析URL的实例,感兴趣的朋友可以参考下,希望对你有所帮助 location对象提供了很多属性和方法用来解析URL. 复制代码代码如下: &l ...
- 剑指offer 判断树是不是对称的
html, body { font-size: 15px; } body { font-family: Helvetica, "Hiragino Sans GB", 微软雅黑, & ...
- Entity Framework 学习初级篇5--ObjectQuery查询及方法
ObjectQuery 类支持对 实体数据模型 (EDM) 执行 LINQ to Entities 和 Entity SQL 查询.ObjectQuery 还实现了一组查询生成器方法,这些方法可用于按 ...
- UL/LI
如何用CSS制作横向菜单 让ul li横向排列及圆点处理 我们先建立一个无序列表,来建立菜单的结构.代码是: <ul><li><a href="1&quo ...
- 基于ATmgea8单片机设计的加热控制系统(转)
源:http://blog.163.com/zhaojun_xf/blog/static/3005058020085102562729/ 1 引言 温度是工业生产中主要的被控参数之一,与之相关的各种温 ...
- zf-关于即将过期提示字符串的修改
Struts2中的 addFieldError(str1,str2); 自带输出 str1= [str2] 这样子是自带输出的 但是如果 要把=[]替换掉怎么办呢 当时想的很复杂,现在知道了,其实很简 ...
- 【树状数组】 poj 2352
题意:给出n个平面二维坐标,对于每个坐标,如果这个坐标跟(0,0)形成的矩形内包含的点数为 k (包含边界,但不包含坐标本身),那么这个坐标就是 level k.输出level 0 - n-1的点数分 ...