题目: http://poj.org/problem?id=3414

很好玩的一个题。关键是又16ms 1A了,没有debug的日子才是好日子。。

 #include <stdio.h>
#include <string.h>
#include <queue>
#include <algorithm>
#include <stack>
using namespace std;
int a, b, c;
bool vis[][];
enum Ways{FILL_A, FILL_B, DROP_A, DROP_B, POUR_AB, POUR_BA}; struct Path
{
int px, py;
Ways way;
}path[][]; struct status
{
int x, y, step;
}; void print_path(int x, int y)
{
stack<struct Path>s;
while(!(x == && y == ))
{
s.push(path[x][y]);
int tx = path[x][y].px;
int ty = path[x][y].py;
x = tx;
y = ty;
}
while(!s.empty())
{
switch(s.top().way)
{
case FILL_A: printf("FILL(1)\n");break;
case FILL_B: printf("FILL(2)\n");break;
case DROP_A: printf("DROP(1)\n");break;
case DROP_B: printf("DROP(2)\n");break;
case POUR_AB: printf("POUR(1,2)\n");break;
case POUR_BA: printf("POUR(2,1)\n");break;
}
s.pop();
}
} queue<struct status>q;
void bfs()
{
while(!q.empty())
{
struct status u = q.front();
q.pop();
if(u.x == c || u.y == c)
{
printf("%d\n", u.step);
print_path(u.x, u.y);
return;
} if(u.x < a && !vis[a][u.y])
{
q.push((struct status){a, u.y, u.step+});
vis[a][u.y] = ;
path[a][u.y] = (struct Path){u.x, u.y, FILL_A};
} if(u.y < b && !vis[u.x][b])
{
q.push((struct status){u.x, b, u.step+});
vis[u.x][b] = ;
path[u.x][b] = (struct Path){u.x, u.y, FILL_B};
} if(u.x > && !vis[][u.y])
{
q.push((struct status){, u.y, u.step+});
vis[][u.y] = ;
path[][u.y] = (struct Path){u.x, u.y, DROP_A};
} if(u.y > && !vis[u.x][])
{
q.push((struct status){u.x, , u.step+});
vis[u.x][] = ;
path[u.x][] = (struct Path){u.x, u.y, DROP_B};
} if(u.x > && u.y < b)
{
int tmp = min(u.x, b-u.y);
if(!vis[u.x-tmp][u.y+tmp])
{
q.push((struct status){u.x-tmp, u.y+tmp, u.step+});
vis[u.x-tmp][u.y+tmp] = ;
path[u.x-tmp][u.y+tmp] = (struct Path){u.x, u.y, POUR_AB};
}
} if(u.x < a && u.y > )
{
int tmp = min(a-u.x, u.y);
if(!vis[u.x+tmp][u.y-tmp])
{
q.push((struct status){u.x+tmp, u.y-tmp, u.step+});
vis[u.x+tmp][u.y-tmp] = ;
path[u.x+tmp][u.y-tmp] = (struct Path){u.x, u.y, POUR_BA};
}
}
}
printf("impossible\n");
} int main()
{
scanf("%d %d %d", &a, &b, &c);
memset(vis, , sizeof(vis));
q.push((struct status){, , });
vis[][] = ;
bfs();
return ;
}

POJ 3414 Pots bfs打印方案的更多相关文章

  1. POJ 3414 Pots ( BFS , 打印路径 )

    题意: 给你两个空瓶子,只有三种操作 一.把一个瓶子灌满 二.把一个瓶子清空 三.把一个瓶子里面的水灌到另一个瓶子里面去(倒满之后要是还存在水那就依然在那个瓶子里面,或者被灌的瓶子有可能没满) 思路: ...

  2. POJ 3414 Pots(BFS)

    Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Description You are g ...

  3. poj 3414 Pots(bfs+输出路径)

    Description You are given two pots, having the volume of A and B liters respectively. The following ...

  4. POJ - 3414 Pots BFS(著名倒水问题升级版)

    Pots You are given two pots, having the volume of A and B liters respectively. The following operati ...

  5. POJ 3414 Pots (BFS/DFS)

    Pots Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7783   Accepted: 3261   Special Ju ...

  6. poj 3414 Pots bfs+模拟

    #include<iostream> #include<cstring> #define fillA 1 #define pourAB 2 #define dropA 3 #d ...

  7. poj 3414 Pots 【BFS+记录路径 】

    //yy:昨天看着这题突然有点懵,不知道怎么记录路径,然后交给房教了,,,然后默默去写另一个bfs,想清楚思路后花了半小时写了120+行的代码然后出现奇葩的CE,看完FAQ改了之后又WA了.然后第一次 ...

  8. BFS POJ 3414 Pots

    题目传送门 /* BFS:六种情况讨论一下,BFS轻松解决 起初我看有人用DFS,我写了一遍,TLE..还是用BFS,结果特判时出错,逗了好长时间 看别人的代码简直是受罪,还好自己终于发现自己代码的小 ...

  9. 广搜+输出路径 POJ 3414 Pots

    POJ 3414 Pots Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13547   Accepted: 5718   ...

随机推荐

  1. Cisco 交换机与路由器故障处理方法分享

    一.网络的复杂性 一般网络包括路由.拨号.交换.视频.WAN(ISDN.帧中继.ATM.…).LAN.VLAN.… 二.故障处理模型 1. 界定问题(Define the Problem) 详细而精确 ...

  2. 编程以外积累: 如何给项目生成类似VS2008的说明文档

    1:[下载] 目前微软提供的官方开源工具 Sandcastle结果跑到项目中一看,抬头就来了这么一段: The Sandcastle CodePlex project is no longer und ...

  3. Android 使用定时器在指定日期及时间执行任务

    public class MainActivity extends Activity { private Handler handler = new Handler(){ @Override publ ...

  4. "ORA-00054: 资源正忙, 但指定以 NOWAIT 方式获取资源, 或者超时失效"的快速解决方法

    引自:http://hi.baidu.com/fynaa/item/c2978952d8d542dfd48bacf6 讲了一大堆: 综合下: 解决方案:select session_id from v ...

  5. 20160410javaweb 开发小案例 --客户管理系统

    客户管理系统---体验基于数据库javaweb的增删改查 添加客户 查询客户列表 修改客户信息 删除客户 条件查询客户信息 分页查询客户 javaee的经典三层架构--工厂类实现解耦 jsp+serv ...

  6. socket的NIO操作

    一.前言 Java中直接使用socket进行通信的场景应该不是很多,在公司的一个项目中有这种需求,所以根据自己的理解和相关资料的参考,基于NIO 实现了一组工具类库,具体的协议还未定义,后续再整理 二 ...

  7. temporary

    private void OnAttendeeConnected(object pObjAttendee) { IRDPSRAPIAttendee pAttendee = pObjAttendee a ...

  8. thinkphp之wampserver安装

    1.如何修改www目录 打开httpd.conf(wamp\bin\apache\Apache2.4.4\conf): 把DocumentRoot "c:/wamp/www" 修改 ...

  9. LIBPNG使用小结(二)

    之前写的LIBPNG库学习小结介绍了怎么样自定义LIBPNG库的write.read.flush函数,而不使用LIBPNG库提供的默认函数. 上一篇讲述的都是在单线程的情况下,今天将程序升级,放在多线 ...

  10. 基于mod_proxy+Apache 2.2.16+Tomcat 7的负载均衡与集群配置

    第一章. 背景简介 对于大多数企业应用,都希望能做到7*24小时不间断运行.要保持如此高的可用性并非易事,比较常见的做法是将系统部署到多台机器上,每台机器都对外提供同样的功能,这就是集群.系统变为集群 ...