查看: 1808| 回复: 7
跳转到指定楼层
上一主题 下一主题
收起左侧

[CareerCup] [第二轮] 3/25-3/31 CareerCup 6.3

全局:

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

x
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.

发帖规范:
http://www.1point3acres.com/bbs/thread-48094-1-1.html
http://www.1point3acres.com/bbs/thread-32423-1-1.html

上一篇:[第二轮] 3/25-3/31 CareerCup 6.2
下一篇:恳请各位大N帮妹纸看看这道python题【数学逻辑不好学编程无力感倍增啊】
🔗
ryancooper 2013-3-26 08:44:45 | 只看该作者
全局:
To generalize this problem, you make the volumes of those two jugs variables. Then we can transform this problem into an implicit graph search problem. If we use BFS, then the answer we find requires the fewest steps. The following is the link to code. https://gist.github.com/ryancooper/12fe286dd4f640a29fe3
I write it in a rush, so it probably contains flaws, but for this particular case, it works just fine. And you can also get the general idea of what i say above.
回复

使用道具 举报

🔗
ryancooper 2013-3-28 04:10:55 | 只看该作者
全局:
I know. But isn't it funny to think of the generalized problem
回复

使用道具 举报

🔗
天道酬勤 2013-3-29 15:03:51 | 只看该作者
回复

使用道具 举报

🔗
vng 2013-3-30 06:06:43 | 只看该作者
全局:
#include <iostream>
#include <stack>

using namespace std;

#define JUG_A   5
#define JUG_B   3

struct 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;
}
回复

使用道具 举报

🔗
vng 2013-3-30 06:07:47 | 只看该作者
全局:
对于 n 大于 5 的情况,可以优化一下:
int tc = 0;
while(n >=5 )
                {
                        cout << " " << 5 << " " << 0 << " " << tc << endl;
                        tc += 5;
                        cout << " " << 0 << " " << 0 << " " << tc << endl;
                        n -=5;
                }
                if( n > 0) { search(n) ;}
                     
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表