您的位置:首页 > 其它

poj3414 Pots BFS

2016-04-24 20:29 225 查看
Pots

Time Limit: 1000MSMemory Limit: 65536K
Total Submissions: 13400Accepted: 5641Special Judge
Description

You are given two pots, having the volume of A and B liters respectively. The following operations can be performed:

FILL(i) fill the pot i (1 ≤ i ≤ 2) from the tap;
DROP(i) empty the pot i to the drain;
POUR(i,j) pour from pot i to pot j; after this operation either the pot j is full (and there may be some water left in the pot i), or the pot i is empty (and all its
contents have been moved to the pot j).

Write a program to find the shortest possible sequence of these operations that will yield exactly C liters of water in one of the pots.

Input

On the first and only line are the numbers A, B, and C. These are all integers in the range from 1 to 100 and C≤max(A,B).

Output

The first line of the output must contain the length of the sequence of operations K. The following K lines must each describe one operation. If there are several sequences of minimal length, output any one of them. If the
desired result can’t be achieved, the first and only line of the file must contain the word ‘impossible’.

Sample Input
3 5 4

Sample Output
6
FILL(2)
POUR(2,1)
DROP(1)
POUR(2,1)
FILL(2)
POUR(2,1)


刚开始想用BFS这么写会不会爆内存,毕竟不去重的话广搜的状态树是指数增长的,结果没有, 而且0MS AC,应该是标记去掉了很多重复的

其实BFS的过程就是个层序遍历状态树的过程(已经将不可能走的节点情况去掉了),深搜则类似于二叉树的前序或中,后序遍历(实际可能遍历一个多叉树)



//poj3414 pots BFS
#include <cstdio>
#include <vector>
#include <cstring>
#include <queue>
#include <iostream>

using namespace std;

const int maxn = 100 + 10;

//路径节点
struct pnode {
char s[2];
int x, y;
};

//BFS队列中的状态节点
struct node {
int a, b, cnt;
vector<pnode> p;
};

int A, B, C, ans;
bool vis[maxn][maxn];
queue<node> Q;

void print(vector<pnode> p) {
printf("%d\n", p.size());
for (int i = 0; i < p.size(); i++) {
switch (p[i].s[0]) {
case 'f': printf("FILL(%d)\n", p[i].x); break;
case 'd': printf("DROP(%d)\n", p[i].x); break;
case 'p': printf("POUR(%d,%d)\n", p[i].x, p[i].y); break;
}
}
}

void fill(int x, node tn) {
pnode pn;
if (x == 1) {
tn.a = A;
}
else if (x == 2) {
tn.b = B;
}
if (!vis[tn.a][tn.b]) {
tn.cnt++;
pn.s[0] = 'f'; pn.s[1] = 0;
pn.x = x;
tn.p.push_back(pn); //新增路径节点
vis[tn.a][tn.b] = true;
Q.push(tn);
}
}

void drop(int x, node tn) {
pnode pn;
if (x == 1) {
tn.a = 0;
}
else if (x == 2) {
tn.b = 0;
}
if (!vis[tn.a][tn.b]) {
tn.cnt++;
pn.s[0] = 'd'; pn.s[1] = 0;
pn.x = x;
tn.p.push_back(pn);
vis[tn.a][tn.b] = true;
Q.push(tn);
}
}

void pour(int x, int y, node tn) {
int ta, tb; pnode pn;

ta = tn.a; tb = tn.b;
if (x == 1) { //pour(1, 2)
if (ta <= B - tb) {
tb += ta;
ta = 0;
}
else {
ta -= (B - tb);
tb = B;
}
}
else { //pour(2, 1)
if (tb <= A - ta) {
ta += tb;
tb = 0;
}
else {
tb -= (A - ta);
ta = A;
}
}
if (!vis[ta][tb]) {
pn.s[0] = 'p'; pn.s[1] = 0;
pn.x = x; pn.y = y;
tn.a = ta; tn.b = tb;
tn.cnt++;
tn.p.push_back(pn);
vis[ta][tb] = true;
Q.push(tn);
}
}

bool bfs() {
while (!Q.empty()) Q.pop();
memset(vis, false, sizeof(vis));

node ns;
ns.a = 0; ns.b = 0; ns.cnt = 0;

vis[0][0] = true;
Q.push(ns);

while (!Q.empty()) {
node tn = Q.front(); Q.pop();
if (tn.a == C || tn.b == C) {
print(tn.p);
return true;
}
fill(1, tn);
fill(2, tn);
drop(1, tn);
drop(2, tn);
pour(1, 2, tn);
pour(2, 1, tn);
}
return false;
}

int main()
{
while (~scanf("%d%d%d", &A, &B, &C)) {
if (!bfs()) {
printf("impossible\n");
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: