• poj 3414 Pots


    题目:

    有二个水壶,对水壶有三种操作:

    1)FILL(i),将i水壶的水填满;

    2)DROP(i),将水壶i中的水全部倒掉;

    3)POUR(i,j)将水壶i中的水倒到水壶j中,若水壶 j 满了,则 i 剩下的就不倒了,问进行多少步操作,并且怎么操作,输出操作的步骤,两个水壶中的水可以达到C这个水量。如果不可能则输出impossible。初始时两个水壶是空的,没有水。

    两个水壶三种操作,所以共六种操作,用广搜来做,需要注意的是记录路径,记录路径时结构体中需要多定义一个指针指向前一个结构体,目的是为了得到这个结构体中的flag即操作。另外这里定义了一个中间变量 t数组,必须得是数组,不然你的指针地址就没得意义了。

    #include <iostream>
    #include <cstdio>
    #include <queue>
    #include <stack>
    #include <cstring>
    using namespace std;
    const int Max = 1e3+10;
    int vis[Max][Max];
    struct cup{
        int x,y;
        int flag;
        int step;
        cup *pre;
    };
    int a,b,e,ans;
    queue<cup> Q;
    stack<int> R;
    void bfs()
    {
        cup c;
        c.x = 0;
        c.y = 0;
        c.step = 1;
        c.flag = -1;
        c.pre = NULL;
        vis[0][0]=1;
        Q.push(c);
        cup t[Max];
        int cnt  = -1;
        while(!Q.empty())
        {
            cnt++;
            t[cnt] = Q.front();Q.pop();
            for(int i=1;i<=6;i++)
            {
                switch(i)
                {
                    case 1:         //fill a
                        c.x = a;
                        c.y = t[cnt].y;
                        c.flag = 1;
                        break;
                    case 2:         //fill b
                        c.x = t[cnt].x;
                        c.y = b;
                        c.flag = 2;
                        break;
                    case 3:     //drop a
                        c.x = 0;
                        c.y = t[cnt].y;
                        c.flag = 3;
                        break;
                    case 4:         //drop b
                        c.x = t[cnt].x;
                        c.y = 0;
                        c.flag = 4;
                        break;
                    case 5:         //pour a 2 b
                        if(t[cnt].x > b-t[cnt].y)
                        {
                            c.x = t[cnt].x-(b-t[cnt].y);
                            c.y = b;
                        }else{
                            c.x = 0;
                            c.y = t[cnt].x + t[cnt].y;
                        }
                        c.flag = 5;
                        break;
                    case 6:     //pour b 2 a
                        if(t[cnt].y > (a-t[cnt].x))
                        {
                            c.x = a;
                            c.y = t[cnt].y - (a-t[cnt].x);
                        }else{
                            c.x=t[cnt].x+t[cnt].y;
                            c.y = 0;
                        }
                        c.flag = 6;
                        break;
                }
                c.step = t[cnt].step+1;
                c.pre = &t[cnt];
                if(vis[c.x][c.y]) continue;
                vis[c.x][c.y] = 1;
                if(c.x==e||c.y==e)
                {
                    ans = t[cnt].step;
                    while(c.pre)
                    {
                        R.push(c.flag);
                        c = *c.pre;
                    }
                    return ;
                }
                Q.push(c);
            }
        }
    }
    void print()
    {
        while(!R.empty())
        {
            int t = R.top();R.pop();
            switch(t)
            {
                case 1: cout<<"FILL(1)"<<endl;break;
                case 2: cout<<"FILL(2)"<<endl;break;
                case 3: cout<<"DROP(1)"<<endl;break;
                case 4: cout<<"DROP(2)"<<endl;break;
                case 5: cout<<"POUR(1,2)"<<endl;break;
                case 6: cout<<"POUR(2,1)"<<endl;break;
            }
        }
    }
    int main()
    {
        memset(vis,0,sizeof(vis));
        cin>>a>>b>>e;
        bfs();
        if(ans==0) cout<<"impossible"<<endl;
        else {
            cout<<ans<<endl;
            print();
        }
        return 0;
    }
    
  • 相关阅读:
    详述Xgboost原理
    Uncaught Error: Script error for "popper.js", needed by: bootstrap
    JS 正则表达式从地址中提取省市县
    OneThink实现多图片批量上传功能
    移动端图片上传解决方案localResizeIMG先压缩后ajax无刷新上传
    PHPCMS V9添加模板自定义全局变量
    CentOS 7:如何安装防火墙?
    HTML5的fieldset标签
    PHP使用CURL详解
    asp.net+swfupload 多图片批量上传(附源码下载)
  • 原文地址:https://www.cnblogs.com/qie-wei/p/12094128.html
Copyright © 2020-2023  润新知