非常可乐BFS搜索

#include<iostream>
#include<cstring>
#include<queue>
using namespace std;
int a,b,c,v[105][105][105],s,n,m;
struct node{
int x,y,z,step;
};
int bfs(int s)
{   int x,y,z; 
queue <node> Q;
node e,next;
e.z=e.y=e.step=0;
e.x=s;
v[e.x][e.y][e.z]=1;
if(e.x==e.y&&e.z==0)
{cout<<e.step<<endl;return 0;}
if(e.x==e.z&&e.y==0)
{cout<<e.step<<endl;return 0;}
if(e.y==e.z&&e.x==0)
{cout<<e.step<<endl;return 0;}
Q.push(e);
while(!Q.empty())
{
e=Q.front();
Q.pop();
for(int i=0;i<6;i++)  
        {  
            if(i==0){
x=e.x-min(e.x,n-e.y);  
                y=e.y+min(e.x,n-e.y); 
z=e.z;
}      //pour(1,2)  
            else if(i==1){
x=e.x+min(e.y,s-e.x);  
                y=e.y-min(e.y,s-e.x); 
z=e.z;
}//pour(2,1)  
            else if(i==2){
x=e.x-min(e.x,m-e.z); 
z=e.z+min(e.x,m-e.z);
              y=e.y;
}//pour(1,3)  
            else if(i==3){
x=e.x+min(e.z,s-e.x);  
                z=e.z-min(e.z,s-e.x);
y=e.y;
}//pour(3,1)  
            else if(i==4)            //pour(3,2)  
            {  
                z=e.z-min(e.z,n-e.y);  
                y=e.y+min(e.z,n-e.y);  
x=e.x;
            }  
            else                     //pour(2,3)  
            {  
                z=e.z+min(e.y,m-e.z);  
                y=e.y-min(e.y,m-e.z);  
x=e.x;
            }  
            if(v[x][y][z])continue;  
            v[x][y][z]=1;  
next.x=x;next.y=y;
next.z=z;next.step=e.step+1;
if(x==y&&z==0)
{cout<<next.step<<endl;return 0;}
if(x==z&&y==0)
{cout<<next.step<<endl;return 0;}
if(y==z&&x==0)
{cout<<next.step<<endl;return 0;}
Q.push(next);
}
}
cout<<"NO"<<endl;
return 0;
}


int main()
{
while(cin>>s>>n>>m&&s&&m&&n)
{
memset(v,0,sizeof(v));
bfs(s);
}
return 0;
}

猜你喜欢

转载自blog.csdn.net/smile__dream/article/details/51872972