波松分酒問題 C++求最優解.

/*
請設計程序解決“波松分酒問題”
問題如下:

某人有12品脫啤酒一瓶,想從中倒出6品脫,但他沒有6品脫的容器,
僅有一個8品脫和一個5品脫的容器,怎樣才能將啤酒分爲兩個6品脫?

抽象分析:

b = 大容器,也表示容積
s = 小容器,也表示容積
(f),(h),(e) 狀態f=滿, e=空, h=數字,表示容量

運算一: b(f) - s(e)  =>  b(b - s), s(f)
變例    b(h) - s(e)  =>  b(h - s), s(f)

運算二: b(e) + s(f)  =>  b(s), s(e)
變例    b(h) + s(f)  =>  b(f), s(s - b + h)

引出    b(f) - s(h)
        b(h) - s(h)

        b(e) + s(h)
        b(h) + s(h)

如果以瓶中酒的數量爲節點, 通過一次以上運算可達到節點之間認爲連通.
此題可轉化爲一個有向圖的搜索問題.
即找出.指定節點(12, 0, 0) 和 (6, 6, 0)之間的最小路徑.

*/
#include <cstdio>
#include <deque>
#include <map>
#include <utility>
#include <queue>

static int big_max_value[] =
{
    12, 8, 12
};
static int small_max_value[] =
{
    8, 5, 5
};
static const int big_offset[] =
{
    0, 1, 0
};
static const int small_offset[] =
{
    1, 2, 2
};


//節點定義
class Node
{
    unsigned char mBig;
    unsigned char mMid;
    unsigned char mSmall;

public:
    static void InitMaxValue(int max1, int max2, int max3)
    {
        big_max_value[0] = max1;
        big_max_value[1] = max2;
        big_max_value[2] = max1;

        small_max_value[0] = max2;
        small_max_value[1] = max3;
        small_max_value[2] = max3;
    }

    Node() : mBig(0), mMid(0), mSmall(0)
    {
    }

    Node(unsigned char a, unsigned char b, unsigned char c) : mBig(a), mMid(b), mSmall(c)
    {
    }


    enum OPCODE
    {
        BIG_OP_MIDDLE               = 0,
        MIDDLE_OP_SMALL,
        BIG_OP_SMALL,
        OP_LAST
    };


    //減運算
    void sub(OPCODE op)
    {
        int big_max = big_max_value[op];
        int small_max = small_max_value[op];

        char& big = *(reinterpret_cast<char*>(this) + big_offset[op]);
        char& small = *(reinterpret_cast<char*>(this) + small_offset[op]);

        if (big > (small_max - small))
        {
            big -= (small_max - small);
            small = small_max;
        }
        else
        {
            small += big;
            big = 0;
        }
    }

    //加運算
    void add(OPCODE op)
    {
        int big_max = big_max_value[op];
        int small_max = small_max_value[op];

        char& big = *(reinterpret_cast<char*>(this) + big_offset[op]);
        char& small = *(reinterpret_cast<char*>(this) + small_offset[op]);

        if (small > big_max - big)
        {
            small -= big_max - big;
            big = big_max;
        }
        else
        {
            big += small;
            small = 0;
        }
    }

    bool check(int value)
    {
        if (mBig == value || mMid == value || mSmall == value)
        {
            return true;
        }
        return false;
    }

    void print() const
    {
        printf("status [%d]=%2d, [%d]=%2d, [%d]=%2dn", big_max_value[0], mBig, big_max_value[1], mMid,
            small_max_value[2], mSmall);
    }

    //相等性判定
    friend bool operator==(Node const & a, Node const & b)
    {
        return memcmp(&a, &b, sizeof(Node)) == 0;
    }

    friend bool operator <(Node const & a, Node const & b)
    {
        return memcmp(&a, &b, sizeof(Node)) < 0;
    }
};


template <class T>
void Search(T start, int value)
{
    typedef std::pair<T, T> NodeValueType;

    typedef std::map<T, T> NodeSet;
    typedef NodeSet::iterator NodeSetIter;

    typedef std::queue<NodeValueType, std::deque<NodeValueType> > NodeQueue;

    NodeSet visited;
    NodeQueue searchQueue;
    NodeValueType last;

    searchQueue.push(std::make_pair(start, start));

    while (!searchQueue.empty())
    {
        NodeValueType cur = searchQueue.front();
        searchQueue.pop();

        visited.insert(cur);
        if (cur.first.check(value))
        {
            last = cur;
            break;
        }

        for (int i = 0; i < Node::OP_LAST; i++)
        {
            Node next1 = cur.first;
            next1.sub(static_cast<Node::OPCODE>(i));

            if (visited.find(next1) == visited.end())
            {
                searchQueue.push(std::make_pair(next1, cur.first));
            }

            Node next2 = cur.first;
            next2.add(static_cast<Node::OPCODE>(i));

            if (visited.find(next2) == visited.end())
            {
                searchQueue.push(std::make_pair(next2, cur.first));
            }
        }
    }

    NodeSetIter cur = visited.find(last.first);

    while (!(cur->first == start))
    {
        cur->first.print();
        cur = visited.find(cur->second);
    }
    cur->first.print();
}

int main()
{
    puts("某人有12品脫啤酒一瓶,想從中倒出6品脫,但他沒有6品脫的容器,n"
         "僅有一個8品脫和一個5品脫的容器,怎樣才能將啤酒分爲兩個6品脫?n");

    for (int i = 0; i < 12; i++)
    {
        printf("---查找取得%d品脫的最少步驟,逆序------------n", i);
        Search(Node(12, 0, 0), i);
    }

    puts("再解一個由13品脫啤酒,卻一個9品脫和一個5品脫的容器n");

    Node::InitMaxValue(13, 9, 5);
    for (int i = 0; i < 12; i++)
    {
        printf("---查找取得%d品脫的最少步驟,逆序------------n", i);
        Search(Node(13, 0, 0), i);
    }
    return 0;
}

實際上的最後一步,結果應是(6,6,0)但事實上我只做到出現一個6的情況.原因是並非所有結果都有兩個相同的值.以下是我做出來的12,8,5的最優解法:
某人有12品脫啤酒一瓶,想從中倒出6品脫,但他沒有6品脫的容器,
僅有一個8品脫和一個5品脫的容器,怎樣才能將啤酒分爲兩個6品脫?

---查找取得0品脫的最少步驟,逆序------------
status [12]=12, [8]= 0, [5]= 0
---查找取得1品脫的最少步驟,逆序------------
status [12]= 1, [8]= 8, [5]= 3
status [12]= 9, [8]= 0, [5]= 3
status [12]= 9, [8]= 3, [5]= 0
status [12]= 4, [8]= 3, [5]= 5
status [12]= 4, [8]= 8, [5]= 0
status [12]=12, [8]= 0, [5]= 0
---查找取得2品脫的最少步驟,逆序------------
status [12]= 2, [8]= 5, [5]= 5
status [12]= 7, [8]= 5, [5]= 0
status [12]= 7, [8]= 0, [5]= 5
status [12]=12, [8]= 0, [5]= 0
---查找取得3品脫的最少步驟,逆序------------
status [12]= 4, [8]= 3, [5]= 5
status [12]= 4, [8]= 8, [5]= 0
status [12]=12, [8]= 0, [5]= 0
---查找取得4品脫的最少步驟,逆序------------
status [12]= 4, [8]= 8, [5]= 0
status [12]=12, [8]= 0, [5]= 0
---查找取得5品脫的最少步驟,逆序------------
status [12]= 7, [8]= 0, [5]= 5
status [12]=12, [8]= 0, [5]= 0
---查找取得6品脫的最少步驟,逆序------------
status [12]= 1, [8]= 6, [5]= 5
status [12]= 1, [8]= 8, [5]= 3
status [12]= 9, [8]= 0, [5]= 3
status [12]= 9, [8]= 3, [5]= 0
status [12]= 4, [8]= 3, [5]= 5
status [12]= 4, [8]= 8, [5]= 0
status [12]=12, [8]= 0, [5]= 0
---查找取得7品脫的最少步驟,逆序------------
status [12]= 7, [8]= 0, [5]= 5
status [12]=12, [8]= 0, [5]= 0
---查找取得8品脫的最少步驟,逆序------------
status [12]= 4, [8]= 8, [5]= 0
status [12]=12, [8]= 0, [5]= 0
---查找取得9品脫的最少步驟,逆序------------
status [12]= 9, [8]= 3, [5]= 0
status [12]= 4, [8]= 3, [5]= 5
status [12]= 4, [8]= 8, [5]= 0
status [12]=12, [8]= 0, [5]= 0
---查找取得10品脫的最少步驟,逆序------------
status [12]=10, [8]= 2, [5]= 0
status [12]= 5, [8]= 2, [5]= 5
status [12]= 5, [8]= 7, [5]= 0
status [12]= 0, [8]= 7, [5]= 5
status [12]= 7, [8]= 0, [5]= 5
status [12]=12, [8]= 0, [5]= 0
---查找取得11品脫的最少步驟,逆序------------
status [12]=11, [8]= 0, [5]= 1
status [12]= 3, [8]= 8, [5]= 1
status [12]= 3, [8]= 4, [5]= 5
status [12]= 8, [8]= 4, [5]= 0
status [12]= 8, [8]= 0, [5]= 4
status [12]= 0, [8]= 8, [5]= 4
status [12]= 4, [8]= 8, [5]= 0
status [12]=12, [8]= 0, [5]= 0
注意這個解法通用性很強,還可以解其它的組合:如最後的13,9,5.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章