//yy:昨天看着这题突然有点懵,不知道怎么记录路径,然后交给房教了,,,然后默默去写另一个bfs,想清楚思路后花了半小时写了120+行的代码然后出现奇葩的CE,看完FAQ改了之后又WA了。然后第一次用对拍去找特殊数据折腾到十二点半终于AC,最后只想感叹没有仔细读题,没办法啊看着英语略烦躁,不扯了,那个题题解不想写了,回到这题。。。今天中午还是花了四十分钟写了这题(好慢啊orz),感觉,啊为什么别人可以一下子就写出来,我却想不到怎么写呢!!!

poj 3414 Pots  【BFS】

题意:两个给定容量a, b的杯子,有倒满水,倒光水,互相倒水三个操作,直到任意一个杯子中的水达到指定容量c为止。输出操作步骤。(a,b,c≤100)

题解:六个方向的bfs,加上100*100的记录路径。

 #include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
#define CLR(a,b) memset((a),(b),sizeof((a)))
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int N = ;
string s[] = {"impossible","FILL(1)","FILL(2)","DROP(1)","DROP(2)","POUR(1,2)","POUR(2,1)"};
int a, b, c;
typedef struct Node {
int x, y;
Node(int _x = , int _y = ):x(_x),y(_y){}
}node;
node g[N][N];//存储上一步的状态
int Do[N][N];//记录操作
bool vis[N][N];//标记
node bfs() {
CLR(g, -); CLR(vis, );
node t;
int i = , j = ;
queue<node> q;
q.push(Node(, ));
while(!q.empty()) {
t = q.front(); q.pop();
if(t.x == c || t.y == c) return t;
vis[t.x][t.y] = ;
if(t.x != a && !vis[a][t.y]) { q.push(Node(a, t.y)); Do[a][t.y] = ; g[a][t.y] = t; }
if(t.y != b && !vis[t.x][b]) { q.push(Node(t.x, b)); Do[t.x][b] = ; g[t.x][b] = t; }
if(t.x && !vis[][t.y]) { q.push(Node(, t.y)); Do[][t.y] = ; g[][t.y] = t; }
if(t.y && !vis[t.x][]) { q.push(Node(t.x, )); Do[t.x][] = ; g[t.x][] = t; }
if(!vis[i = max(, t.x + t.y - b)][j = min(b, t.x + t.y)]) { q.push(Node(i, j)); Do[i][j] = ; g[i][j] = t; }
if(!vis[i = min(a, t.x + t.y)][j = max(, t.x + t.y - a)]) { q.push(Node(i, j)); Do[i][j] = ; g[i][j] = t; }
}
return Node(, );
}
void dfs(const node & d, const int & p) {
if(d.x + d.y == ) {
if(p) { cout << p << endl; } else { cout << s[] << endl; }
return;
}
dfs(g[d.x][d.y], p+);
cout << s[Do[d.x][d.y]] << endl;
}
int main() {
scanf("%d%d%d", &a, &b, &c);
node ans = bfs();
dfs(ans, );
return ;
}

poj 3414 Pots 【BFS+记录路径 】的更多相关文章

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

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

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

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

  3. Pots POJ - 3414 (搜索+记录路径)

    Pots Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 22688   Accepted: 9626   Special J ...

  4. POJ 3414 Pots bfs打印方案

    题目: http://poj.org/problem?id=3414 很好玩的一个题.关键是又16ms 1A了,没有debug的日子才是好日子.. #include <stdio.h> # ...

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

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

  6. POJ 3414 Pots(BFS)

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

  7. POJ 3414 Pots (BFS/DFS)

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

  8. poj 3414 Pots bfs+模拟

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

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

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

随机推荐

  1. hdu 3794 Magic Coupon

    浙大计算机研究生保研复试上机考试-2011年  贪心: 注意:输入输出用scanf  printf 可以加快速度,用cin WA #include<iostream> #include&l ...

  2. 架构实战项目心得(十):基于spring-ladp的统一用户中心结构设计以及代码结构设计

    一.目录设计 1 公司 2 部门 3 注册人员 4 层级人员 二.规则 1 注册 自行注册人员放到模拟公司的目录下,等所属公司组织结构建立完毕,将此人员迁移到所属公司(或者删除此人员,所属公司新建此人 ...

  3. 获取URL中某个参数的值

    JS代码: function getQueryString(name){ var reg = new RegExp("(^|&)" + name + "=([^& ...

  4. WPF 资源字典

    使用好处:存储需要被本地话的内容(错误消息字符串等,实现软编码),减少重复的代码,重用样式,实现多个项目之间的共享资源;修改一个地方所有引用的地方都会被修改,方便统一风格; 使用方法,归纳起来主要有下 ...

  5. html中行级元素的居中显示。

    垂直居中.以label标签为例. <style> #label1{ vertical-align:middle; line-height:40px;<*父元素的height*> ...

  6. PS基础,英语

    PS基础(矢量图案的绘制) 水平参考线:新建背景(长宽一致,背景内容为透明)---设置水平参考线(水平垂直都要)---完成. 背景制作:设置前景色---用矩形选框工具绘制正方形选区(背景已被参考线平分 ...

  7. javaweb之jsp指令

    1.JSP指令简介 JSP指令是为JSP引擎设计的,它们并不直接产生任何可见输出,而只是告诉引擎如何处理JSP页面中的其余部分. 在JSP 2.0规范中共定义了三个指令:page指令,Include指 ...

  8. go install runtime/cgo: open /usr/local/go/pkg/darwin_amd64/runtime/cgo.a: permission denied

    在做更新时,收到下面提示: go get  github.com/astaxie/beego go install runtime/cgo: open /usr/local/go/pkg/darwin ...

  9. Spring课程 Spring入门篇 5-6 introductions应用

    1 解析 1.1 aop:declare-parents 标签简介 1.2 标签使用样式 2 代码演练 2.1 introductions标签应用 1 解析 1.1 aop:declare-paren ...

  10. Linux基础之命令练习Day7-nginx,nfs

    一. Nginx Nginx("engine x") 是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP服务器.Nginx是由Igor Sysoev为俄罗 ...