POJ 3414 bfs求路径(建bfs树然后树上寻找路径)

Pots
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 12556 Accepted: 5293 Special 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)

题意:
对两杯水一共有6中操作(分别倒满,分别倒空,1倒向2里,2倒向1里),问到达要求水量的最短操作数操作路径
解法:
此题思考比较久,然后想到lrj提到的关于bfs求最短路径将路径先全部打出来组织一颗bfs,然后从找到的底端一直往上找到顶端,将沿途的节点全部压栈,然后全部弹出即可,因为自己对指针比较熟,所以这里用了动态建树的方法。没有写删除树的操作就提交了。 担心内存泄漏,结果没有爆内存。 万幸的1A了。

#include <iostream>
#include <cstdio>
#include <queue>
#include <set>
#include <stack>
#include <cstdlib>

using namespace std;

typedef struct node{
    int v,h;
    int ma;
    node* pre;
}Po;

int A,B,C;
queue<Po*>qu;
stack<int>st;
char SS[7][20] = {"DROP(1)","DROP(2)","FILL(1)","FILL(2)","POUR(1,2)","POUR(2,1)"};

Po* bfs(Po Si){
    set<int>se;
    qu.push(&Si);
    Po* term;
    se.insert(0);
    int a,b,i;

    while(!qu.empty()){
        term = qu.front();qu.pop();
        a = term->v/1000;
        b = term->v%1000;
        if(a == C||b == C) return term;
        for(i = 0;i<6;i++){
            a = term->v/1000;
            b = term->v%1000;
                if(i == 0){
                    Po* newPo0 = (Po*)malloc(sizeof(Po));;
                    a = 0;
                    newPo0->v = a*1000 + b;
                    if(se.count(newPo0->v) == 0){
                        se.insert(newPo0->v);
                        newPo0->h = term->h + 1;
                        newPo0->pre = term;
                        newPo0->ma = i;
                        qu.push(newPo0);
                    }
                }
                if(i == 1){
                    Po* newPo1 = (Po*)malloc(sizeof(Po));;
                    b = 0;
                    newPo1->v = a*1000 + b;
                    if(se.count(newPo1->v) == 0){
                        se.insert(newPo1->v);
                        newPo1->h = term->h + 1;
                        newPo1->pre = term;
                        newPo1->ma = i;
                        qu.push(newPo1);
                    }
                }
                if(i == 2){
                    Po* newPo2 = (Po*)malloc(sizeof(Po));;
                    a = A;
                    newPo2->v = a*1000 + b;
                    if(se.count(newPo2->v) == 0){
                        se.insert(newPo2->v);
                        newPo2->h = term->h + 1;
                        newPo2->pre = term;
                        newPo2->ma = i;
                        qu.push(newPo2);
                    }
                }
                if(i == 3){
                    Po* newPo3 = (Po*)malloc(sizeof(Po));
                    b = B;
                    newPo3->v = a*1000 + b;
                    if(se.count(newPo3->v) == 0){
                        se.insert(newPo3->v);
                        newPo3->h = term->h + 1;
                        newPo3->pre = term;
                        newPo3->ma = i;
                        qu.push(newPo3);
                    }
                }
                if(i == 4){
                    Po* newPo4 = (Po*)malloc(sizeof(Po));
                    if(B-b == 0);
                    else if(B-b>0){
                        if(a - (B-b)>=0){
                            a -= B-b;
                            b = B;
                        }else{
                            b += a;
                            a = 0;
                        }
                    }
                    newPo4->v = a*1000 + b;
                    if(se.count(newPo4->v) == 0){
                        se.insert(newPo4->v);
                        newPo4->h = term->h + 1;
                        newPo4->pre = term;
                        newPo4->ma = i;
                        qu.push(newPo4);
                    }
                }
                if(i == 5){
                    Po* newPo5 = (Po*)malloc(sizeof(Po));
                    if(A-a == 0);
                    else if(A-a>0){
                        if(b - (A-a)>=0){
                            b -= A-a;
                            a = A;
                        }else{
                            a += b;
                            b = 0;
                        }
                    }
                    newPo5->v = a*1000 + b;
                    if(se.count(newPo5->v) == 0){
                        se.insert(newPo5->v);
                        newPo5->h = term->h + 1;
                        newPo5->pre = term;
                        newPo5->ma = i;
                        qu.push(newPo5);
                    }
                }
        }
    }
    return NULL;
}

void dfs(Po* E){
    while(E!=NULL){
        st.push(E->ma);
        E = E->pre;
    }
}

int main(){
    while(scanf("%d%d%d",&A,&B,&C)!=EOF){
        while(!qu.empty()) qu.pop();
        while(!st.empty()) st.pop();
        Po Si;
        Si.h = Si.v = 0;
        Si.pre = NULL;
        Si.ma = -1;
        Po *E = bfs(Si);
        if(E == NULL)
            printf("impossible\n");
        else{
            printf("%d",E->h);
            dfs(E);
            while(!st.empty()){
                int k = st.top();st.pop();
                printf("%s\n",SS[k]);
            }
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值