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;
}
原文地址:https://www.cnblogs.com/wintersong/p/5239095.html