poj 3414 pots [经典 BFS ]

本文探讨了智能算法在大数据开发领域的应用,包括Hadoop、Spark等技术的使用及优化策略。
#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;
}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值