题目传送门

 /*
BFS:六种情况讨论一下,BFS轻松解决
起初我看有人用DFS,我写了一遍,TLE。。还是用BFS,结果特判时出错,逗了好长时间
看别人的代码简直是受罪,还好自己终于发现自己代码的小错误:)
*/
/************************************************
Author :Running_Time
Created Time :2015-8-3 14:17:24
File Name :POJ_3414_BFS.cpp
**************************************************/ #include <cstdio>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <cstring>
#include <cmath>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <bitset>
#include <cstdlib>
#include <ctime>
using namespace std; #define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
typedef long long ll;
const int MAXN = 1e4 + ;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + ;
struct Point {
int a, b, step;
int op[MAXN];
};
bool vis[][];
int A, B, C;
int ans; void BFS(void) {
memset (vis, false, sizeof (vis));
queue<Point> Q; Q.push ((Point) {, , });
while (!Q.empty ()) {
Point p = Q.front (); Q.pop ();
if (p.a == C || p.b == C) {
printf ("%d\n", p.step);
for (int i=; i<=p.step; ++i) {
if (p.op[i] == ) puts ("FILL(1)");
else if (p.op[i] == ) puts ("FILL(2)");
else if (p.op[i] == ) puts ("DROP(1)");
else if (p.op[i] == ) puts ("DROP(2)");
else if (p.op[i] == ) puts ("POUR(1,2)");
else if (p.op[i] == ) puts ("POUR(2,1)");
}
return ;
}
Point tmp;
if (p.a < A && !vis[A][p.b]) {
vis[A][p.b] = true;
tmp = p; tmp.a = A; tmp.op[++tmp.step] = ; //FILL1
Q.push (tmp);
}
if (p.b < B && !vis[p.a][B]) {
vis[p.a][B] = true;
tmp = p; tmp.b = B; tmp.op[++tmp.step] = ; //FILL2
Q.push (tmp);
}
if (p.a > && !vis[][p.b]) {
vis[][p.b] = true;
tmp = p; tmp.a = ; tmp.op[++tmp.step] = ; //DROP1
Q.push (tmp);
}
if (p.b > && !vis[p.a][]) {
vis[p.a][] = true;
tmp = p; tmp.b = ; tmp.op[++tmp.step] = ; //DROP2
Q.push (tmp);
}
if (p.a > && p.b < B) {
int t = min (p.a, B - p.b);
if (!vis[p.a-t][p.b+t]) {
vis[p.a-t][p.b+t] = true; //POUR1->2
tmp = p; tmp.a -= t; tmp.b += t; tmp.op[++tmp.step] = ;
Q.push (tmp);
}
}
if (p.b > && p.a < A) {
int t = min (p.b, A - p.a);
if (!vis[p.a+t][p.b-t]) {
vis[p.a+t][p.b-t] = true; //POUR2->1
tmp = p; tmp.a += t; tmp.b -= t; tmp.op[++tmp.step] = ;
Q.push (tmp);
}
}
} puts ("impossible");
} int main(void) { //POJ 3414 Pots
while (scanf ("%d%d%d", &A, &B, &C) == ) {
BFS ();
} return ;
}

BFS POJ 3414 Pots的更多相关文章

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

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

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

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

  3. POJ 3414 Pots(罐子)

    POJ 3414 Pots(罐子) Time Limit: 1000MS    Memory Limit: 65536K Description - 题目描述 You are given two po ...

  4. poj 3414 Pots【bfs+回溯路径 正向输出】

    题目地址:http://poj.org/problem?id=3414 Pots Time Limit: 1000MS   Memory Limit: 65536K Total Submissions ...

  5. POJ 3414 Pots

    Pots Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status  ...

  6. POJ 3414 Pots【bfs模拟倒水问题】

    链接: http://poj.org/problem?id=3414 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22009#probl ...

  7. poj 3414 Pots(广搜BFS+路径输出)

    转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents 题目链接:id=3414">http://poj.org/probl ...

  8. poj 3414 Pots ( bfs )

    题目:http://poj.org/problem?id=3414 题意:给出了两个瓶子的容量A,B, 以及一个目标水量C, 对A.B可以有如下操作: FILL(i)        fill the ...

  9. POJ 3414 Pots bfs打印方案

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

随机推荐

  1. 算法(1):查找&排序

    算法(Algorithm):一个计算过程,解决问题的方法 程序 = 数据结构+算法 时间复杂度: 当算法过程中出现循环折半的时候,复杂度式子中会出现 O(logn) 时间复杂度小结: 1. 时间复杂度 ...

  2. key通用操作【九】

    一.概述: 在该系列的前几篇博客中,主要讲述的是与Redis数据类型相关的命令,如String.List.Set.Hashes和Sorted-Set.这些命令都具有一个共同点,即所有的操作都是针对与K ...

  3. CURL不可以读写文件

    最近在学ES(elastic search),参考http://www.learnes.net/里面翻译的官方权威指南(后面发现官网已经推出了中文版文档了).里面有的例子把访问ES的命令做了简化如下: ...

  4. JSP点击计数器

    以下内容引用自http://wiki.jikexueyuan.com/project/jsp/hits-counter.html: 一个点击计数器能得知关于网站某个特定页面的访问量.假设人们第一次登陆 ...

  5. Servlet的Cookies处理

    以下内容引用自http://wiki.jikexueyuan.com/project/servlet/cookies-handling.html: Cookies是存储在客户端计算机上的文本文件,用于 ...

  6. 采用jmeter测试dubbo服务接口

    http://www.kissyu.org/2017/02/08/jmeter%E6%B5%8B%E8%AF%95dubbo%E6%8E%A5%E5%8F%A3/

  7. apache ab 測试 apr_socket_connect(): 因为目标机器积极拒绝 无法连接

    遇到这样的情况通常是你开的并行数量太多了... 比如:ab -c 1000 -n 10000 http://localhost/index.html 如此大的请求就会挂掉,只是还是有补救措施的,能够通 ...

  8. Android内存泄露之开篇

    先来想这三个问题 内存泄露是怎么回事 内存会泄露的原因 避免内存泄露 1.内存泄露怎么回事 一个程序中,已经不须要使用某个对象,可是由于仍然有引用指向它垃圾回收器就无法回收它,当然该对象占用的内存就无 ...

  9. C - The C Answer (2nd Edition) - Exercise 1-8

    /* Write a program to count blanks, tabs, and newlines. */ #include <stdio.h> /* count blanks, ...

  10. script标签async和defer的区别及作用

    作用: 1.没有 defer 或 async,浏览器会立即加载并执行指定的脚本,也就是说不等待后续载入的文档元素,读到就加载并执行. 2.async 属性表示异步执行引入的 JavaScript,与 ...