BFS POJ 3414 Pots
/*
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的更多相关文章
- poj 3414 Pots 【BFS+记录路径 】
//yy:昨天看着这题突然有点懵,不知道怎么记录路径,然后交给房教了,,,然后默默去写另一个bfs,想清楚思路后花了半小时写了120+行的代码然后出现奇葩的CE,看完FAQ改了之后又WA了.然后第一次 ...
- 广搜+输出路径 POJ 3414 Pots
POJ 3414 Pots Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13547 Accepted: 5718 ...
- POJ 3414 Pots(罐子)
POJ 3414 Pots(罐子) Time Limit: 1000MS Memory Limit: 65536K Description - 题目描述 You are given two po ...
- poj 3414 Pots【bfs+回溯路径 正向输出】
题目地址:http://poj.org/problem?id=3414 Pots Time Limit: 1000MS Memory Limit: 65536K Total Submissions ...
- POJ 3414 Pots
Pots Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit Status ...
- POJ 3414 Pots【bfs模拟倒水问题】
链接: http://poj.org/problem?id=3414 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22009#probl ...
- poj 3414 Pots(广搜BFS+路径输出)
转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents 题目链接:id=3414">http://poj.org/probl ...
- poj 3414 Pots ( bfs )
题目:http://poj.org/problem?id=3414 题意:给出了两个瓶子的容量A,B, 以及一个目标水量C, 对A.B可以有如下操作: FILL(i) fill the ...
- POJ 3414 Pots bfs打印方案
题目: http://poj.org/problem?id=3414 很好玩的一个题.关键是又16ms 1A了,没有debug的日子才是好日子.. #include <stdio.h> # ...
随机推荐
- 守护进程详解及创建,daemon()使用
一,守护进程概述 Linux Daemon(守护进程)是运行在后台的一种特殊进程.它独立于控制终端并且周期性地执行某种任务或等待处理某些发生的事件.它不需要用户输入就能运行而 且提供某种服务,不是对整 ...
- msp430入门编程00
msp430单片机最小系统 msp430入门学习 msp430入门编程
- Ajax核心知识(1)
XMLHttpRequest对象创建 所有现代浏览器均支持XMLHttpRequest对象( IE5 和 IE6 使用 ActiveXObject). XMLHttpRequest用于在后台与服务器交 ...
- MongoDB小结08 - update【$pull】
它可以删除所匹配的值,如果[1,1,2,1] 执行pull 1 后,只剩下[2]
- how to read openstack code
本文的目的不是介绍openstack.我们这里假设你已经知道了openstack是什么,能够做什么.所以目的是介绍如何阅读openstack的代码.通过读代码来进一步学习openstack. 转载要求 ...
- 重置网络命令win7
开始→运行→输入:CMD 点击确定(或按回车键),打开命令提示符窗口. 在命令提示符中输入:netsh winsock reset (按回车键执行命令) 稍后,会有成功的提示:成功地重置Winsock ...
- Python输入输出及其他
print用法 print会输出一个\n,也就是换行符,这样光标移动到了下一行行首,接着输出,之前已经通过stdout输出的东西依旧保留,而且保证我们在下面看到最新的输出结果.回车 \r 本义是光标重 ...
- win7下 sublime text2操作快捷键 - leafu
Ctrl+L 选择整行(按住-继续选择下行) Ctrl+KK 从光标处删除至行尾 ...
- android application类简单介绍(一)
每次应用程序执行时.应用程序的application类保持实例化的状态. 通过扩展applicaiton类,能够完毕下面3项工作: 1.对android执行时广播的应用程序级事件如低低内做出响应. 2 ...
- Nginx + FastCgi + Spawn-fcgi + C 架构的server环境搭建
1.Nginx 1.1.安装 Nginx 的中文维基 http://wiki.codemongers.com/NginxChs 下载 Nginx 0.6.26(开发版)(请下载最新版本号) tar z ...