[POJ-3414]Pots

6 篇文章 0 订阅
#include<iostream>
#include<iomanip>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<ctype.h>
#include<algorithm>
#include<queue>
#include<vector>

using namespace std;
const int maxn = 100 + 12;

struct Node {
    int a, b;
    int t;
    int s, f;
    Node(int aa, int bb, int tt, int ss, int ff): a(aa), b(bb), t(tt), s(ss), f(ff) { }
};
struct Order {
    string st;
    int s;
    int f;
    Order(string sst, int ss, int ff): st(sst), s(ss), f(ff) { }
};

bool vis[maxn][maxn];
vector<Order> V;


void Print( int a ) {
    if( V[a].f != 0 ) { Print(V[a].f); }
    cout << V[a].st << endl;
}

int main() {
    int A, B, C;
    while( cin >> A >> B >> C ) {
        memset( vis, 0, sizeof(vis) );
        queue<Node> Q;
        V.clear();
        Q.push( Node(0,0,0,0,-1) );
        V.push_back( Order("NULL",0,-1) );
        vis[0][0] = 1;
        int mark = -1;
        int cnt = 0;
        while( !Q.empty() ) {
            Node q = Q.front();
            if( q.a==C || q.b==C ) {
                cout << q.t << endl;
                mark = q.s;
                break;
            }
            for(int i=0; i<6; i++) {
                int ta, tb;
                if( i==0 ) {
                    ta = A; tb = q.b;
                    if( vis[ta][tb] ) { continue; }
                    ++cnt;
                    vis[ta][tb] = 1;
                    Q.push( Node(ta,tb,q.t+1,cnt,q.s) );
                    V.push_back( Order("FILL(1)",cnt,q.s) );
                }
                if( i==1 ) {
                    ta = q.a; tb = B;
                    if( vis[ta][tb] ) { continue; }
                    ++cnt;
                    vis[ta][tb] = 1;
                    Q.push( Node(ta,tb,q.t+1,cnt,q.s) );
                    V.push_back( Order("FILL(2)",cnt,q.s) );
                }
                if( i==2 ) {
                    ta = 0; tb = q.b;
                    if( vis[ta][tb] ) { continue; }
                    ++cnt;
                    vis[ta][tb] = 1;
                    Q.push( Node(ta,tb,q.t+1,cnt,q.s) );
                    V.push_back( Order("DROP(1)",cnt,q.s) );
                }
                if( i==3 ) {
                    ta = q.a; tb = 0;
                    if( vis[ta][tb] ) { continue; }
                    ++cnt;
                    vis[ta][tb] = 1;
                    Q.push( Node(ta,tb,q.t+1,cnt,q.s) );
                    V.push_back( Order("DROP(2)",cnt,q.s) );
                }
                if( i==4 ) {
                    if( q.a >= B-q.b ) { ta = q.a-(B-q.b); tb=B; }
                    else { tb = q.b+q.a; ta=0; }
                    if( vis[ta][tb] ) { continue; }
                    ++cnt;
                    vis[ta][tb] = 1;
                    Q.push( Node(ta,tb,q.t+1,cnt,q.s) );
                    V.push_back( Order("POUR(1,2)",cnt,q.s) );
                }
                if( i==5 ) {
                    if( q.b >= A-q.a ) { tb = q.b-(A-q.a); ta=A; }
                    else { ta = q.a+q.b; tb=0; }
                    if( vis[ta][tb] ) { continue; }
                    ++cnt;
                    vis[ta][tb] = 1;
                    Q.push( Node(ta,tb,q.t+1,cnt,q.s) );
                    V.push_back( Order("POUR(2,1)",cnt,q.s) );
                }
            }
            Q.pop();
        }
        if( mark == -1 ) { cout << "impossible" << endl; }
        else {
            Print( mark );
        }
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值