码迷,mamicode.com
首页 > 其他好文 > 详细

poj3414 Pots

时间:2021-01-25 11:05:02      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:lan   pos   搜索   ring   inline   set   poj   break   需要   

搜索空间不过 \(1e4\) ,暴力 \(bfs\) 即可得到最少操作。

输出每次操作可能需要手动模拟栈,方便找到最优解后回溯输出。

/**
* poj3414 Pots
*
*/

#include <cstdio>
#include <queue>
#include <iostream>
#include <cstring>
#include <stack>
using namespace std;

const int N = 1e4+5;
bool book[101][101];
struct node{int a, b, step, op, fa;} Q[N];
int bfs(int A, int B, int C)
{
    memset(book, 0, sizeof book);
    int head = 0;
    int tail = 0;
    Q[tail++] = node{0, 0, 0, 0, -1};
    book[0][0] = 1;
    while (head != tail) {
        //    cout << head << ‘#‘;
        node t = Q[head];
        if (t.a == C || t.b == C) {
            return head;
        }

        if (!book[A][t.b]) {  // FILL(1)
            Q[tail++] = node{A, t.b, t.step+1, 11, head};
            book[A][t.b] = 1;
        }
        if (!book[t.a][B]) {  // FILL(2)
            Q[tail++] = node{t.a, B, t.step+1, 12, head};
            book[t.a][B] = 1;
        }
        if (!book[0][t.b]) {  // DROP(1)
            Q[tail++] = node{0, t.b, t.step+1, 21, head};
            book[0][t.b] = 1;
        }
        if (!book[t.a][0]) {  // DROP(2)
            Q[tail++] = node{t.a, 0, t.step+1, 22, head};
            book[t.a][0] = 1;
        }

        // DROP(1,2)
        int a = max(0, t.a-B+t.b);
        int b = min(B, t.a+t.b);
        if (!book[a][b]) {
            Q[tail++] = node{a, b, t.step+1, 31, head};
            book[a][b] = 1;
        }

        // DROP(2,1)
        a = min(A, t.a+t.b);
        b = max(0, t.b-A+t.a);
        if (!book[a][b]) {
            Q[tail++] = node{a, b, t.step+1, 32, head};
            book[a][b] = 1;
        }
        ++head;
    }
    return -1;
}

char out[][20] = {
    "FILL(1)", "FILL(2)", "DROP(1)", "DROP(2)", "POUR(1,2)", "POUR(2,1)"
};

int parse(int op)
{
    switch(op)
    {
        case 11: return 0; break;
        case 12: return 1; break;
        case 21: return 2; break;
        case 22: return 3; break;
        case 31: return 4; break;
        case 32: return 5; break;
    }
    return -1;
}

int main()
{
    int A, B, C;
    while (cin >> A >> B >> C) {
        int pos = bfs(A, B, C);
        if (pos == -1) {
            puts("impossible");
            continue;
        }
        stack<int> st;
        while (pos != 0) {
            st.push(Q[pos].op);
            pos = Q[pos].fa;
        }
        cout << st.size() << endl;
        while (!st.empty()) {
            // cout << st.top() << endl;
            int t = st.top();
            cout << out[parse(t)] << endl;
            st.pop();
        }
    }

    return 0;
}

poj3414 Pots

标签:lan   pos   搜索   ring   inline   set   poj   break   需要   

原文地址:https://www.cnblogs.com/zbhfz/p/14316503.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!