zoukankan      html  css  js  c++  java
  • HDU 1495 非常可乐 bfs 难度:1

    http://acm.hdu.edu.cn/showproblem.php?pid=1495

    第三个杯子的盛水量可由前两个杯子得到,而前两个杯子状态总数在100*100以内,穷举可实现

    #include <cstdio>
    #include <cstring>
    #include <queue>
    using namespace std;
    const int mxind=101;
    const int maxn=mxind*mxind;
    int n,m,s;
    int dp[maxn];
    queue<int>que;
    void poursub(int &f,int &t,int ct){
            int sub=ct-t;
            if(f<sub){
                    t+=f;
                    f-=f;
            }
            else {
                    f-=sub;
                    t+=sub;
            }
    }
    int pour(int fs,int fn,int fm,int op){
            int ts=fs,tn=fn,tm=fm;
            switch(op){
            case 0:
                    poursub(ts,tn,n);
            break;
            case 1:
                    poursub(ts,tm,m);
            break;
            case 2:
                    poursub(tn,ts,s);
            break;
            case 3:
                    poursub(tn,tm,m);
            break;
            case 4:
                    poursub(tm,ts,s);
            break;
            case 5:
                    poursub(tm,tn,n);
            break;
            }
            return ts*mxind+tn;
    }
    int main(){
            while(scanf("%d%d%d",&s,&m,&n)==3&&s){
                    if((s&1)==1){
                            puts("NO");
                            continue;
                    }
                    memset(dp,0x7f,sizeof(dp));
                    while(!que.empty())que.pop();
                    int ss=s*mxind;
                    dp[ss]=0;
                    que.push(ss);
                    bool fl=false;
                    while(!que.empty()){
                            int f=que.front();que.pop();
                            int fs=f/mxind,fn=f%mxind,fm=s-fs-fn;
                            if(fs==s/2&&(fn==s/2||fm==s/2)){
                                    printf("%d
    ",dp[fs*mxind+fn]);
                                    fl=true;
                                    break;
                            }
                            for(int i=0;i<6;i++){
                                    int tmp=pour(fs,fn,fm,i);
                                    if(dp[tmp]>dp[f]+1){
                                            dp[tmp]=dp[f]+1;
                                            que.push(tmp);
                                    }
                            }
                    }
                    if(!fl)puts("NO");
            }
            return 0;
    }
    

      

  • 相关阅读:
    五种I/O模型
    Python socket服务
    Python 协程
    python openpyxl 简单使用
    python 文件夹压缩
    Python 多进程
    MySQL 自定义函数
    python 队列
    python 多线程、线程锁、事件
    python paramiko模块:远程连接服务器
  • 原文地址:https://www.cnblogs.com/xuesu/p/4338201.html
Copyright © 2011-2022 走看看