九度OJ;題目1147:Jugs,九度oj1147jugs

來源:互聯網
上載者:User

九度OJ;題目1147:Jugs,九度oj1147jugs

原題連結地址:http://ac.jobdu.com/problem.php?pid=1147

轉載請註明本文連結:http://blog.csdn.net/yangnanhai93/article/details/42016353


BFS很簡單的思想,但是注意剪枝,因為很多會重複,比如,不斷的empty,這個重複很嚴重,所以很有必要去除重複,即記錄1000 *1000的矩陣,保證對想通的a,b不重複計算

#include <stdio.h>#include <queue>#include <string>#include <memory.h>using namespace std; string op[6]={"fill A","fill B","pour B A","pour A B","empty A","empty B"};bool visited[1001][1001];struct Node{    int left,right;    vector<int> op;};void Cal(int a,int b,int q){    queue<Node> result;    Node first,second;    first.left=0;    first.right=0;    result.push(first);    memset(visited,0,sizeof(visited));    while(!result.empty())    {        first=result.front();        result.pop();        for(int i=0;i<6;i++)        {            second=first;            switch (i)            {            case 0:                second.left=a;                break;            case 1:                second.right=b;                break;            case 2://pour b to a                if(second.right<=a-second.left)                {                    second.left=second.left+second.right;                    second.right=0;                }                else                {                    second.right=second.right-(a-second.left);                    second.left=a;                                  }                break;            case 3:                if(second.left<=b-second.right)                {                    second.right=second.right+second.left;                    second.left=0;                }                else                {                    second.left=second.left-(b-second.right);                    second.right=b;                }                break;            case 4:                second.left=0;                break;            case 5:                second.right=0;                break;            }            second.op.push_back(i);            if(second.right==q)            {                for(int i=0;i<second.op.size();i++)                    printf("%s\n",op[second.op[i]].c_str());                printf("success\n");                return;            }            else            {                if(!visited[second.left][second.right])                    result.push(second);                visited[second.left][second.right]=true;            }        }    }}int main(){    int a,b,q;    while(scanf("%d%d%d",&a,&b,&q)!=EOF)    {        Cal(a,b,q);    }    return 0;}/**************************************************************    Problem: 1147    User: vincent_ynh    Language: C++    Result: Accepted    Time:10 ms    Memory:2036 kb****************************************************************/


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.