ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

poj3414 Pots

2021-01-23 09:36:42  阅读:138  来源: 互联网

标签:int poj3414 Pots pos bfs tail book include


搜索空间不过 \(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;
}

标签:int,poj3414,Pots,pos,bfs,tail,book,include
来源: https://www.cnblogs.com/zbhfz/p/14316503.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有