zoukankan      html  css  js  c++  java
  • HDU 1495 BFS

    根据题意,既然瓶子没有刻度,那么它们直观的刻度就是他们各自本身的容量。

    一开始的想法漏洞太大,想着填满其中一个容器就行,咳咳。。。 没有仔细想它的模拟过程。  

    答题思路:

    分别看看当前容器能否往外倒,即  >0  如果能的话,看看能倒在哪个容器里。既然只有3个容器,直接暴力就好啦。写的有点麻木。。。

    #include<iostream>
    #include<string>
    #include<string.h>
    #include<stdio.h>
    #include<queue>
    #include<math.h>
    using namespace std;
    int s,n,m,med;
    int vis[110][110][110];
    struct node{
        int tot;
        int x;int y;
        int step;
        friend bool operator < (node a,node b){
            return a.step > b.step;  //升序
        }
    };
    queue<node> q;
    int legal(node t ){
        int cnt=0;
        if(t.tot==med){
            cnt++;
        }
        if(t.x==med){
            cnt++;
        }
        if(t.y==med){
            cnt++;
        }
        if(cnt==2){
            return true;
        }
        return false;
    }
    bool bfs(int &ans){
        node temp,next;
        while(!q.empty()){
            temp=q.front();
            q.pop();
            //cout<<temp.tot<<" "<<temp.x<<" "<<temp.y<<endl;
            if(legal(temp)){
                ans=temp.step;
                return true;
            }
            if(temp.tot>0){
                if(temp.x<n){
                int r=min(temp.tot,n-temp.x);
                next.tot=temp.tot-r;
                next.x=temp.x+r;
                next.y=temp.y;
                next.step=temp.step+1;
                q.push(next);
                if(next.step<vis[next.tot][next.x][next.y]){
                    vis[next.tot][next.x][next.y]=next.step;
                    q.push(next);
                }
             }
             if(temp.y<m){
                int r=min(temp.tot,m-temp.y);
                next.tot=temp.tot-r;
                next.x=temp.x;
                next.y=temp.y+r;
                next.step=temp.step+1;
               if(next.step<vis[next.tot][next.x][next.y]){
                    vis[next.tot][next.x][next.y]=next.step;
                    q.push(next);
                }
    
             }
            }
            if(temp.x>0){
                    if(temp.tot<s){
                        int r=min(temp.x,s-temp.tot);
                        next.tot=temp.tot+r;
                        next.x=temp.x-r;
                        next.y=temp.y;
                        next.step=temp.step+1;
                    if(next.step<vis[next.tot][next.x][next.y]){
                    vis[next.tot][next.x][next.y]=next.step;
                    q.push(next);
                }
            }
                    if(temp.y<m){
                        int r=min(temp.x,m-temp.y);
                        next.tot=temp.tot;
                        next.x=temp.x-r;
                        next.y=temp.y+r;
                        next.step=temp.step+1;
                         if(next.step<vis[next.tot][next.x][next.y]){
                       vis[next.tot][next.x][next.y]=next.step;
                       q.push(next);
                }
            }
    
    
            }
            if(temp.y>0){
                    if(temp.tot<s){
                    int r=min(temp.y,s-temp.tot);
                    next.tot=temp.tot+r;
                    next.x=temp.x;
                    next.y=temp.y-r;
                     next.step=temp.step+1;
                         if(next.step<vis[next.tot][next.x][next.y]){
                       vis[next.tot][next.x][next.y]=next.step;
                       q.push(next);
                }
                    }
                    if(temp.x<n){
                        int r=min(temp.y,n-temp.x);
                            next.tot=temp.tot;
                            next.x=temp.x+r;
                            next.y=temp.y-r;
                             next.step=temp.step+1;
                         if(next.step<vis[next.tot][next.x][next.y]){
                       vis[next.tot][next.x][next.y]=next.step;
                       q.push(next);
                }
                        }
                    }
            }
    
    
        return false;
    }
    int main(){
    
        while(cin>>s>>n>>m){
            if(s==0&&n==0&&m==0) break;
            memset(vis,120,sizeof(vis));
            while(!q.empty()) q.pop();
            if(s%2==1){
                cout<<"NO"<<endl;
                continue;
            }else{
                node tt;
                tt.tot=s;tt.x=0;tt.y=0;tt.step=0;
                q.push(tt);
                med=s/2;
                vis[s][0][0]=0;
                int ans;
                if(bfs(ans)){
                    cout<<ans<<endl;
                }
                else{
                    cout<<"NO"<<endl;
                }
            }
        }
    
        return 0;
    }
  • 相关阅读:
    Luogu 1080 【NOIP2012】国王游戏 (贪心,高精度)
    Luogu 1314 【NOIP2011】聪明的质检员 (二分)
    Luogu 1315 【NOIP2011】观光公交 (贪心)
    Luogu 1312 【NOIP2011】玛雅游戏 (搜索)
    Luogu 1525 【NOIP2010】关押罪犯 (贪心,并查集)
    Luogu 1514 引水入城 (搜索,动态规划)
    UVA 1394 And Then There Was One / Gym 101415A And Then There Was One / UVAlive 3882 And Then There Was One / POJ 3517 And Then There Was One / Aizu 1275 And Then There Was One (动态规划,思维题)
    Luogu 1437 [HNOI2004]敲砖块 (动态规划)
    Luogu 1941 【NOIP2014】飞扬的小鸟 (动态规划)
    HDU 1176 免费馅饼 (动态规划)
  • 原文地址:https://www.cnblogs.com/wintersong/p/5239095.html
Copyright © 2011-2022 走看看