hdu 1495

#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<vector>
#include<queue>
using namespace std;
struct node
{
    int s,n,m,tm;
};
bool vis[105][105][105];
queue<node> q;
void bfs(int vs,int vn,int vm)
{
    node top,v,t;
    while(!q.empty())
        q.pop();
    memset(vis,0,sizeof(vis));
    //v={vs,0,0,0};
    v.m=0,v.n=0,v.s=vs,v.tm=0;
    q.push(v);
   // vis[vs][0][0]=1;
    while(!q.empty())
    {
        top=q.front();
        q.pop();
        vis[top.s][top.n][top.m]=1;
        //printf("tops:%d\ntopn:%d\ntopm:%d\ntoptm:%d\n",top.s,top.n,top.m,top.tm);
        if((top.s==vs/2 && top.n==vs/2)||(top.s==vs/2 && top.m==vs/2)||(top.n==vs/2 && top.m==vs/2))
        {
           // ans=top.tm;
           printf("%d\n",top.tm);
            return ;
        }
       // node t={top.s,top.n,top.m,top.tm};
        t.m=top.m,t.n=top.n,t.s=top.s,t.tm=top.tm;
        //printf("ts:%d\ntn:%d\ntm:%d\nttm:%d\n",t.s,t.n,t.m,t.tm);
        if(top.s)
        {
           // printf("yes\n");
            //s->n
            if(top.s>vn-top.n)//n will be full
            {
                t.m=top.m;
                t.n=vn;
                t.s=top.s-(vn-top.n);
                t.tm=top.tm+1;
                //q.push(t);
            }
            else
            {
                t.m=top.m;
                t.n=top.n+top.s;
                t.s=0;//s will be empty
                t.tm=top.tm+1;
                //q.push(t);
            }
            if(!vis[t.s][t.n][t.m])
            {
               // vis[t.s][t.n][t.m]=1;
                q.push(t);
                vis[t.s][t.n][t.m]=1;
            }
            //===========================================================//
           // s->m
            if(top.s>vm-top.m)//m will be full
            {
                t.n=top.n;
                t.m=vm;
                t.s=top.s-(vm-top.m);
                t.tm=top.tm+1;
               // q.push(t);
            }
            else
            {
                t.n=top.n;
                t.m=top.m+top.s;
                t.s=0;// s will be empty
                t.tm=top.tm+1;
                //q.push(t);
            }
            if(!vis[t.s][t.n][t.m])
            {
                // vis[t.s][t.n][t.m]=1;
                q.push(t);
                vis[t.s][t.n][t.m]=1;
            }
        }
        //-----------------------------------------------------------------------//
        if(top.n)
        {
            //n->s
            if(top.n>vs-top.s)//s will be full
            {
                t.m=top.m;
                t.s=vs;
                t.n=top.n-(vs-top.s);
                t.tm=top.tm+1;
               // q.push(t);
            }
            else
            {
                t.m=top.m;
                t.s=top.s+top.n;
                t.n=0;//n will be empty
                t.tm=top.tm+1;
              //  q.push(t);
            }
            if(!vis[t.s][t.n][t.m])
            {
                 // vis[t.s][t.n][t.m]=1;
                q.push(t);
                vis[t.s][t.n][t.m]=1;
            }
            //==============================================================================================
           //n->m
            if(top.n>vm-top.m)
            {
                t.s=top.s;
                t.m=vm;//m will be full
                t.n=top.n-(vm-top.m);
                t.tm=top.tm+1;
               // q.push(t);
            }
            else
            {
                t.s=top.s;
                t.m=top.m+top.n;
                t.n=0;// n will be empty
                t.tm=top.tm+1;
               // q.push(top);
            }
            if(!vis[t.s][t.n][t.m])
            {
                 // vis[t.s][t.n][t.m]=1;
                q.push(t);
                vis[t.s][t.n][t.m]=1;
            }
        }
        //------------------------------------------------------------------------------------------------------------
        if(top.m)
        {
            //m->s
            if(top.m>vs-top.s)
            {
                t.n=top.n;
                t.s=vs;// s will be full
                t.m=top.m-(vs-top.s);
                t.tm=top.tm+1;
               // q.push(t);
            }
            else
            {
                t.n=top.n;
                t.s=top.s+top.m;
                t.m=0;//m will be empty
                t.tm=top.tm+1;
               // q.push(t);
            }
            if(!vis[t.s][t.n][t.m])
            {
                // vis[t.s][t.n][t.m]=1;
                q.push(t);
                vis[t.s][t.n][t.m]=1;
            }
            //===========================================================================================================
            //m->n
            if(top.m>vn-top.n)
            {
                t.s=top.s;
                t.n=vn;// n will be full
                t.m=top.m-(vn-top.n);
                t.tm=top.tm+1;
                //q.push(top);
            }
            else
            {
                t.s=top.s;
                t.n=top.n+top.m;
                t.m=0;//m will be empty
                t.tm=top.tm+1;
                //q.push(t);
            }
            if(!vis[t.s][t.n][t.m])
            {
                // vis[t.s][t.n][t.m]=1;
                q.push(t);
                vis[t.s][t.n][t.m]=1;
            }
        }
    }
    printf("NO\n");
    return ;
}
int main()
{
    int s,n,m;
    while(~scanf("%d%d%d",&s,&n,&m) && s && n && m)
    {
        if(s%2==1)
        {
            printf("NO\n");
            continue;
        }
        bfs(s,n,m);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值