BFS POJ 3414 Pots

时间:2022-01-09 07:25:08

题目传送门

 /*
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 ;
}