[kuangbin带你飞]专题一 简单搜索- M - 非常可乐

#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
#define INF 0x3f3f3f
int gcd(int a, int b) {return b==0 ? a:gcd(b,a%b);}

int s, n, m;
int flag;
int vis[110][110][110];

struct node
{
    int x, y, z,step;
    node(){}
    node (int x, int y, int z, int step):x(x),y(y),z(z),step(step){}
};
void bfs()
{
    memset(vis,0,sizeof(vis));
    queue<node>q;
    node p(s,0,0,0);
    vis[s][0][0] = 1;
    q.push(p);
    while(!q.empty())
    {
        p = q.front(); q.pop();
        if((p.x==p.y&&p.x==s/2)||(p.y==p.z&&p.y==s/2)||(p.x==p.z&&p.x==s/2)){
            flag = 1;
            printf("%d\n", p.step);
            break;
        }   
        int x = p.x; int y = p.y; int z = p.z;

        node a;

        // x->y
        if(x!=0){
            a.z = z; a.step = p.step+1;
            if(x+y<=n){
                a.x = 0; a.y = x+y;
            }else{
                a.x = x-(n-y); a.y = n;
            }
            if(!vis[a.x][a.y][a.z]){
                vis[a.x][a.y][a.z] = 1;
                q.push(a);
            }
        }

        // y->x
        if(y!=0){
            a.z = z; a.step = p.step+1;
            if(x+y<=s){
                a.x = x+y; a.y = 0;
            }else{
                a.x = s; a.y = y-(s-x);
            }
            if(!vis[a.x][a.y][a.z]){
                vis[a.x][a.y][a.z] = 1;
                q.push(a);
            }
        }

        // x->z
        if(x!=0){
            a.y = y; a.step = p.step+1;
            if(x+z<=m){
                a.x = 0; a.z = x+z;
            }else{
                a.x = x-(m-z); a.z = m;
            }
            if(!vis[a.x][a.y][a.z]){
                vis[a.x][a.y][a.z] = 1;
                q.push(a);
            }
        }

        // z->x
        if(z!=0){
            a.y = y; a.step = p.step+1;
            if(x+z<=s){
                a.x = x+z; a.z = 0;
            }else{
                a.x = s; a.z = z-(s-x);
            }
            if(!vis[a.x][a.y][a.z]){
                vis[a.x][a.y][a.z] = 1;
                q.push(a);
            }
        }

        // y->z;
        if(y!=0){
            a.x = x; a.step = p.step+1;
            if(y+z<=m){
                a.y = 0; a.z = y+z;
            }else{
                a.y = y-(m-z); a.z = m;
            }
            if(!vis[a.x][a.y][a.z]){
                vis[a.x][a.y][a.z] = 1;
                q.push(a);
            }
        }

        // z->y
        if(z!=0){
            a.x = x; a.step = p.step+1;
            if(y+z<=n){
                a.y = y+z; a.z = 0;
            }else{
                a.y = n; a.z = z-(n-y);
            }
            if(!vis[a.x][a.y][a.z]){
                vis[a.x][a.y][a.z] = 1;
                q.push(a);
            }
        }
    }
}

int main()
{
    while(~scanf("%d %d %d", &s, &n, &m)&&s&&n&&m)
    {
        if(s&1){
            printf("NO\n");
            continue;
        }
        flag = 0;
        bfs();
        if(!flag) printf("NO\n");
    }   
}

你可能感兴趣的:(kuangbin)