H - Pots
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<queue>
using namespace std; #define maxn 110 int A, B, C;
char a[][] ={ "FILL(1)","FILL(2)","DROP(1)","DROP(2)","POUR(1,2)","POUR(2,1)"}; struct node
{
int fromX, fromY, step, op;
}v[maxn][maxn];
struct point
{
int x, y;
};//当前瓶子A,B的水量 //输出路径
void DFS(int x, int y)
{
if(x == && y == )
return ; DFS(v[x][y].fromX, v[x][y].fromY); printf("%s\n", a[ v[x][y].op ]); }
void BFS()
{
queue<point> Q;
point s, q;
s.x = s.y = ;
v[][].step = ; Q.push(s); while(Q.size())
{
s = Q.front();Q.pop(); if(s.x == C || s.y == C)
{
printf("%d\n", v[s.x][s.y].step-);
DFS(s.x, s.y); return ;
} for(int i=; i<; i++)
{
q = s; if(i == )
q.x = A;
else if(i == )
q.y = B;
else if(i == )
q.x = ;
else if(i == )
q.y = ;
else if(i == )
{
if(q.x+q.y <= B)
q.y += q.x, q.x = ;//把A里面的水全倒B里面
else
q.x -= (B-q.y), q.y = B;//把B倒满
}
else
{
if(q.x+q.y <= A)
q.x += q.y, q.y = ;
else
q.y -= (A-q.x), q.x = A;
} if(v[q.x][q.y].step == )
{
v[q.x][q.y].step = v[s.x][s.y].step+;
v[q.x][q.y].fromX = s.x;
v[q.x][q.y].fromY = s.y;
v[q.x][q.y].op = i; Q.push(q);
}
}
} printf("impossible\n");
} int main()
{
while(scanf("%d%d%d", &A, &B, &C) != EOF)
{
memset(v, , sizeof(v)); BFS();
} return ;
}
H - Pots的更多相关文章
- (poj)3414 Pots (输出路径的广搜)
Description You are given two pots, having the volume of A and B liters respectively. The following ...
- POJ 3414 Pots【bfs模拟倒水问题】
链接: http://poj.org/problem?id=3414 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22009#probl ...
- Pots(bfs)
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8266 Accepted: 3507 Special Judge D ...
- poj 3414 Pots (bfs+线索)
Pots Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10071 Accepted: 4237 Special J ...
- M - Pots
You are given two pots, having the volume of A and B liters respectively. The following operations c ...
- POJ3414—Pots(bfs加回溯)
http://poj.org/problem?id=3414 Pots Time Limit: 1000MS Memor ...
- POJ 3414 Pots(BFS+回溯)
Pots Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11705 Accepted: 4956 Special J ...
- poj 3414 Pots【bfs+回溯路径 正向输出】
题目地址:http://poj.org/problem?id=3414 Pots Time Limit: 1000MS Memory Limit: 65536K Total Submissions ...
- poj3414 Pots (BFS)
Pots Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12198 Accepted: 5147 Special J ...
随机推荐
- iOS中JavaScript和OC交互
转载自:http://www.devzeng.com/blog/ios-uiwebview-interaction-with-javascript.html 还可参考的文章:http://blog.c ...
- CI 框架增加公用函数-如何使用Helper辅助函数
在CI框架增加一个公用的函数,或者说是要在页面上调用一个函数,可以写一个帮助类如:menu_helper.php.类名必有_helper后缀名,这标识为帮助类.文件要放在application/hel ...
- javascript——函数属性和方法
<script type="text/javascript"> //每个函数都包含两个属性:length 和 prototype //length:当前函数希望接受的命 ...
- 第七篇、Nginx Install On Mac
方式一: 在mac上安装nginx,依次安装对应的依赖 pcre ./configure --prefix=/usr/local/pcre-8.37 --libdir=/usr/local/lib/p ...
- java.io.serializable
为什么要实现 java.io.serializable? 简单点:“好处就是将来项目如果要做集群的话,就实现java.io.serializable接口”
- 关于在DEDECMS当中模板文件不存在的解决方案
大家可能在生成文档的时候遇到过,模板文件不存在,无法解析 这个问题,其实这个遇到这个问题的大多数人应该是修改了默认模板的名称才导致这样的问题,如果你避免这种问题大家在一开始对模板进行命名的时候就要写 ...
- CodeFirst中DB保存时报错:对一个或多个实体的验证失败。
错误提示如下: 开始以为有字段可能没有添加数据,可是检查了很久,仍然没有任何头绪. 后使用DbEntityValidationException进行调试,问题才得以解决
- 使用JAVA客户端对HDFS进行代码编写(五)
在linux中,在JAVA中编程,耗时的不是代码的编写而是环境的搭建,版本的选择...日了苍天,昨天eclipse突然抽风在linux运行不起来,耗了几个小时,试了各种办法...现在windows环境 ...
- iOS手势识别的详细使用(拖动,缩放,旋转,点击,手势依赖,自定义手势)
iOS手势识别的详细使用(拖动,缩放,旋转,点击,手势依赖,自定义手势) 1.UIGestureRecognizer介绍 手势识别在iOS上非常重要,手势操作移动设备的重要特征,极大的增加 ...
- eclipse问题解决(maven插件link方式安装失败)
一.link方式安装eclipse的一款插件:maven (附:若不熟悉link方式,则进入此处:link方式安装eclipse插件) 其间,只弹出警告,大概意思是:部分内容,未经授权,谨慎使 ...