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> # ...
随机推荐
- 【转】Intellij IDEA 快捷键大全
IntelliJ Idea 常用快捷键列表 Ctrl+Shift + Enter,语句完成“!”,否定完成,输入表达式时按 “!”键Ctrl+E,最近的文件Ctrl+Shift+E,最近更改的文件Sh ...
- 使用idea编译spring-framework5.0源码
自从迈入java开发这个行当,从来没有好好的研究过源码,深感惭愧,话不多说,今天上一篇使用idea编译spring5.0源码. 以下在win中构建和编译过程分为 jdk环境的配置 gradle的下载和 ...
- mysql配置文件my.ini的修改问题
修改innodb_buffer_pool_size这个参数一般都没问题,但是修改innodb_log_file_size这个参数如果过大,mysql日志就会提示: Error: log file .\ ...
- MongoDB小结08 - update【$pull】
它可以删除所匹配的值,如果[1,1,2,1] 执行pull 1 后,只剩下[2]
- Orcle定时生成表数据作业
--建表 create table table41( id ) not null, --主键 col1 ), col2 ), col3 ), col4 int, col5 timestamp, col ...
- linux 实现VLAN
本文将在一台linux机器上,利用linuxbridge 等技术模拟创建VLAN 环境. 首先,创建vlan interface ip link add link ens33 name ens33.8 ...
- 解决confluence的乱码问题
使用confluence时发现一些含有中文的页面中,中文都变成了问号. 继续搜索解决方案,发现时数据库中数据的格式不对, 在mysql中输入以下命令: mysql> show variabl ...
- java.util.Scanner
java.util.Scanner是Java5的新特征,主要功能是简化文本扫描.这个类最实用的地方表现在获取控制台输入,其他的功能都很鸡肋,尽管Java API文档中列举了大量的API方法,但是都不怎 ...
- JS原生DOM操作总结
DOM的主要操作——增.删.改.查节点 (1) 查找节点 document.getElementById('div1') document.getElementsByName('uname') doc ...
- C# .NET想要另存一个项目,sln文件丢了怎么办
如下图所示,我想要另存一个工程,把 V4.4整个的项目另存为V4.5,我可以把解决方案文件(.sln)改名字,但是我没法把文件夹改名字,改了打开sln就说找不到. 很简单的一个思路是反正sln是多 ...