#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
#include <stack>
#include <memory.h>
#include <cstdio>
#define Bug cout << "here\n";
using namespace std;
const int N = 101;
struct node {
int k1, k2, steps;
int op;
node *pre;
}que[N*N];
node cur, nex;
int vis[N][N];
char str[][10] = {"FILL(1)", "DROP(1)", "FILL(2)", "DROP(2)", "POUR(1,2)", "POUR(2,1)"};
void DFS(node* t) {
if(t->pre != NULL) {
DFS(t->pre);
cout << str[t->op] << endl;
}
}
void BFS(int v1, int v2, int c) {
que[0] = cur;
vis[0][0] = 1; int yi;
int head = 0, tail = 1;
node* cp;
while(head < tail) {
cp = &que[head++];
if(cp->k1 == c || cp->k2 == c) {
printf("%d\n", cp->steps);
DFS(cp);
return;
}
for(int i = 0; i < 6; i++) {
switch(i) {
case 0 : nex.k1 = v1; nex.k2 = cp->k2; break; // A 满
case 1 : nex.k1 = 0; nex.k2 = cp->k2; break; // A 空
case 2 : nex.k1 = cp->k1; nex.k2 = v2; break; // B 满
case 3 : nex.k1 = cp->k1; nex.k2 = 0; break; // B 空
case 4 : yi = cp->k1 + cp->k2 - v2;
if(yi > 0) {
nex.k1 = yi;
nex.k2 = v2;
}
else {
nex.k1 = 0;
nex.k2 = yi+v2;
}
break;
case 5 : yi = cp->k1 + cp->k2 - v1;
if(yi > 0) {
nex.k1 = v1;
nex.k2 = yi;
}
else {
nex.k1 = yi+v1;
nex.k2 = 0;
}
}
nex.steps = cp->steps + 1;
nex.pre = cp;
nex.op = i;
if(!vis[nex.k1][nex.k2]) {
que[tail++] = nex;
vis[nex.k1][nex.k2] = 1;
}
}
}
cout << "impossible" << endl;
}
int main() {
int v1, v2, c;
while(scanf("%d%d%d", &v1, &v2, &c) == 3) {
memset(vis, 0, sizeof(vis));
BFS(v1, v2, c);
}
return 0;
}poj 3414 pots [经典 BFS ]
最新推荐文章于 2020-09-10 11:11:10 发布
本文探讨了智能算法在大数据开发领域的应用,包括Hadoop、Spark等技术的使用及优化策略。
748

被折叠的 条评论
为什么被折叠?



