5 升 3 升 容器,產生任何數量的水的演算法實現

來源:互聯網
上載者:User

You have a five-quart jug, a three-quart jug, and an unlimited supply of water (but no measuring cups). How would you come up with exactly
four quarts of water? Note that the jugs are oddly shaped, such that filling up exactly "half" of the jug would be impossible.

演算法不夠最佳化,可以實現產生任何容量的水。 但是如果輸入的水容量過大,會導致計算時間過程。

日後有時間,在考慮其他的實現,以及最佳化。放在這裡,只是作為一個記錄。

#include <iostream>#include <stack>using namespace std;#define JUG_A   5#define JUG_B   3struct node{        struct node * parent;        struct node * childs;        struct node * next;        int a;  /* jug a */        int b;  /* jug b */        int c; /* total of summary */        node (node* pNode)        {                a = pNode->a;                b = pNode->b;                c = pNode->c;                parent = pNode;                childs = NULL;                next = NULL;        }        node ()        {                parent = NULL;                childs = NULL;                next = NULL;                a = 0;                b = 0;                c = 0;        }        bool OK(int n) {                if(a == n || b == n || c == n                /*|| a+b == n || a + c == n || b + c == n*/)                {                        return true;                }                return false;        }};void printNode(const node * pNode){        cout << " " << pNode->a << " " << pNode->b << " " << pNode->c << endl;}node *fillNode(node* pNode ){        int t;        node * tmp;        /* fill up jug a */        node* pFillJugA  = new node(pNode);        pNode->childs = pFillJugA;        tmp = pFillJugA;        pFillJugA->a = JUG_A ;        /* fill up jug b */        node * pFillJugB = new node(pNode);        tmp->next = pFillJugB;        tmp = pFillJugB;        pFillJugB->b = JUG_B;        if(pNode->a > 0)        {                /* from jug a to b */                node *pFromAToB = new node(pNode);                tmp->next = pFromAToB ;                tmp = pFromAToB;                t = JUG_B - pFromAToB->b;                pFromAToB->a -= t;                pFromAToB->b += t;        }        if(pNode->b > 0)        {                /* from jug b to a */                node * pFromB2A = new node(pNode);                tmp->next = pFromB2A;                tmp = pFromB2A;                t = JUG_A - pFromB2A->a ;                t = t > pFromB2A->b ? pFromB2A->b : t;                pFromB2A->a += t;                pFromB2A->b -= t;        }        if(pNode->a > 0)        {                /* from a to c */                node * pFromA2C = new node(pNode);                tmp->next = pFromA2C;                tmp = pFromA2C;                pFromA2C->c += pFromA2C->a;                pFromA2C->a = 0;        }        if(pNode->b > 0)        {                /* from b to c */                node * pFromB2C = new node(pNode);                tmp->next = pFromB2C;                tmp = pFromB2C;                pFromB2C->c += pFromB2C->b;                pFromB2C->b = 0;        }        return pFillJugA;}void destroyNode(stack<node *> &sn){        node * first, *tmp;        while(!sn.empty())        {                first = sn.top();                sn.pop();                while(first)                {                        tmp = first->next;                        delete first;                        first = tmp;                }        }}void search(int n){        stack<node *> sNode;        node * cur =  new node();        sNode.push(cur);        bool flag = false;        node * first , * tmp;        do        {                first = NULL;                cur = sNode.top();                while(cur)                {                        if(cur->OK(n)) {flag =true; break;}                        node * retnode = fillNode(cur);                        if(first == NULL) { first = retnode;}                        else {                                tmp = first;                                while(tmp->next)                                {                                        tmp = tmp->next;                                }                                tmp->next = retnode;                        }                        cur = cur->next;                }                sNode.push(first);        } while(!flag);        stack<node*> outPutNode;        while(cur)        {                outPutNode.push(cur);                cur = cur->parent;        }        cout <<endl;        while(!outPutNode.empty())        {                printNode(outPutNode.top());                outPutNode.pop();        }        destroyNode(sNode);}int main(){        int n ;        cout << endl<< "Please input the quart you want to get : " ;        while(cin >> n)        {                if(n <= 0) { break;}                search(n);                cout << endl << "Please input the quart you want to get : " ;        }        return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.