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 ...
随机推荐
- 网站如何防Session冒名顶替和cookie防篡改
做网站难免要面对安全性的问题,诸如sql注入拉,cookie冒名拉,等等,sql注入算是老生常谈,翻翻旧账有不少优秀的帖子在说明这个问题,所以我们来说说Session冒名顶替的风险以及应对的办法. 首 ...
- 谈一下关于C++函数包装问题
在C++中,我们经常遇到在某个特定的时刻,需要将函数进行包装调用,尤其是当我们需要将不同签名的函数放到同一个集合时,由于函数签名不一致导致我们不能直接将各式各样的函数指针放到诸如list这样的集合中, ...
- Unity3d脚本的生命周期
接下来,做出一下讲解:最先执行的方法是Awake,这是生命周期的开始,用于进行激活时的初始化代码,一般可以在这个地方将当前脚本禁用:this.enable=false,如果这样做了,则会直接跳转到On ...
- 关于js中alert弹出窗口换行!
请用"\n" 如果这个不可以的话就是"\\n" 比如: <script type="text/javascript"> al ...
- CSS3 box-shadow(阴影使用)
from: http://jingyan.baidu.com/article/03b2f78c4d9fae5ea237aea6.html css3 box-shadow 内阴影与外阴影 1- box- ...
- C# 程序性能提升篇-2、类型(字段类型、class和struct)的错误定义所影响性能浅析
前景提要: 编写程序时,也许你不经意间,就不知不觉的定义了错误的类型,从而发生了额外的性能消耗,从而降低了效率,不要说就发生那么一次两次,如果说是程序中发生了循环.网络程序(不断请求处理的)等这些时候 ...
- smali文件语法参考
Dalvik opcodes Author: Gabor Paller Vx values in the table denote a Dalvik register. Depending on th ...
- 单例模式——使用GCD实现单例模式 & 非ARC单例模式 &使用GCD和线程锁实现单例模式-b
1.单利模式概述 链接: iOS开发懒汉模式&恶寒模式 2.使用GCD实现单利模式 2.1新建一个project,然后新建一个HMDataTool类展示GCD实现单例模式 #import & ...
- 字符串处理---统计每一行字符串当中的字符“u”个数
package com.guoxiaoming.string; import java.io.BufferedReader; import java.io.FileInputStream; impor ...
- Yeoman:适合现代Web应用的现代工作流
Yeoman:适合现代Web应用的现代工作流 Yeoman是Google的团队和外部贡献者团队合作开发的,他的目标是通过Grunt(一个用于开发任务自动化的命令行工具)和Bower(一个HTML. ...