题目: 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. mac jdbc连接mysql

    1.下载jdbc驱动: http://dev.mysql.com/downloads/connector/j/ 2.增加jdbc的jar包至项目的libs文件夹并build path 2.改动环境变量 ...

  2. jQuery -&gt; 获取/设置/删除DOM元素的属性

    jQuery的属性操作很easy,以下以一个a元素来说明属性的获取/设置/删除操作 <body> <a>jquery.com</a> </body> 加 ...

  3. php笔记04:get/post请求有两种主要方式

    get/post的区别有哪些? 1. 安全性get请求的数据会显示在地址栏上,post请求的数据,放在http协议的消息体中   2. 从可以提交的数据大小来看:   http协议本身并没有限制数据大 ...

  4. Java8特性---关于Null

    为了防止无良网站的爬虫抓取文章,特此标识,转载请注明文章出处.LaplaceDemon/SJQ. http://www.cnblogs.com/shijiaqi1066/p/5713941.html ...

  5. Java json工具类,jackson工具类,ObjectMapper工具类

    Java json工具类,jackson工具类,ObjectMapper工具类 >>>>>>>>>>>>>>> ...

  6. nyoj 32 组合数

    组合数 时间限制:3000 ms  |            内存限制:65535 KB 难度:3   描述 找出从自然数1.2.... .n(0<n<10)中任取r(0<r< ...

  7. Android 自定义Gallery浏览图片

    之前写的<Android ImageSwitcher和Gallery的使用>一文中提到我在教室一下午为实现那个效果找各种资料.期间在网上找了一个个人觉得比较不错的效果,现在贴图上来: 其实 ...

  8. WebSocket使用教程 - 带完整实例

    http://my.oschina.net/u/1266171/blog/357488 什么是WebSocket?看过html5的同学都知道,WebSocket protocol 是HTML5一种新的 ...

  9. C#删除微信自定义菜单

    删除 string access_token = "你的token"; string posturl = "https://api.weixin.qq.com/cgi-b ...

  10. js简单日期获取( 菜鸟入门基础)

    关于js日期的获取要用到最基本的Date()方法获取当日的日期 var d =new Date();  //定义日期对象 var y=d.getFullYear();   //获取年 var m=d. ...