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 ...
随机推荐
- 让ie6/7/8兼容css3的圆角阴影等特殊效果的方法 PIE1.0.0及placeholder在这些IE下生效的方法
PIE地址:http://css3pie.com/ 使用方法1: #login,#AnnouncementBox { border:3px solid #fff; -webkit-border-r ...
- xcode7 icon图标设置
- 解决Xcode7多个模拟器的方法
xcode模拟器都这样显示,没办法判断是哪个系统,解决办法是 1.关闭xcode 2.终端输入 sudo killall -9 com.apple.CoreSimulator.CoreSimulato ...
- 寒哥教你学 iOS - 经验漫谈(转)
转自http://www.cocoachina.com/ios/20150907/13339.html 本篇文章主要讲解 4个问题 load妙用 aop面向切面编程 NSNumber Or Int @ ...
- 了解负载均衡 会话保持 session同步(转)
一,什么负载均衡 一个新网站是不要做负载均衡的,因为访问量不大,流量也不大,所以没有必要搞这些东西.但是随着网站访问量和流量的快速增长,单台服务器受自身硬件条件的限制,很难承受这么大的访问量.在这种情 ...
- mysql 语句资料总结
一.UNION命令 UNION 操作符用于合并两个或多个 SELECT 语句的结果集. 请注意,UNION 内部的 SELECT 语句必须拥有相同数量的列.列也必须拥有相似的数据类型.同时,每条 SE ...
- 使用Raphael 画图(二) 扩展的图形 (javascript)
看这文章前,建议先看第一编文章<使用Raphael 画图(一) 基本图形 (javascript)>. 在Raphael基础上扩展的图形: 要运行该例子要引入附件的2个js包.(g.rap ...
- python json string和dict的转化
__author__ = 'SRC_TJ_XiaoqingZhang' import json data1 = {'b': 789, 'c': 456, 'a': 123} encode_json = ...
- JavaScript不可变原始值和可变的对象引用
一.JavaScript不可变原始值 JavaScript中的原始值(undefined,null,布尔值,数字和字符串)与对象(包括了数组和函数)有着根本的区别.原始值是不可变的(undefined ...
- 得到指定进程PID
//#include "targetver.h" #include "stdio.h" #include <windows.h> #include ...