hdu1495 非常可乐(简单BFS)

点击打开链接

#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <queue>
using namespace std;

bool vis[103][103][103];
struct Status{
    int s,n,m;
    int step;
};
int s,n,m;
int half;
Status st,cur,temp;

bool Ok(Status s){
    int num=0;
    if(s.m==half) num++;
    if(s.s==half) num++;
    if(s.n==half) num++;
    if(num==2) return true;
    return false;
}
Status Oper(int a){
            Status t=cur;
            switch(a){
                case 0: t.s>(n-t.n)?t.s-=(n-t.n),t.n=n:(t.n+=t.s,t.s=0);break;//S->N
                case 1: t.n>(s-t.s)?t.n-=(s-t.s),t.s=s:(t.s+=t.n,t.n=0);break;//N->S
                case 2: t.s>(m-t.m)?t.s-=(m-t.m),t.m=m:(t.m+=t.s,t.s=0);break;//S->M
                case 3: t.m>(s-t.s)?t.m-=(s-t.s),t.s=s:(t.s+=t.m,t.m=0);break;//M->S
                case 4:t.n>(m-t.m)?t.n-=(m-t.m),t.m=m:(t.m+=t.n,t.n=0);break;//N->M
                case 5:t.m>(n-t.n)?t.m-=(n-t.n),t.n=n:(t.n+=t.m,t.m=0);break;//M->N
            }
            return t;
}
int bfs(){
    queue<Status>q;
    q.push(st);
    while(!q.empty()){
        cur=q.front();
        q.pop();
        if(Ok(cur)) return cur.step;
        for(int i=0;i<6;i++){
                temp=Oper(i);
                if(!vis[temp.m][temp.n][temp.s]){
                    vis[temp.m][temp.n][temp.s]=1;
                    temp.step=cur.step+1;
                    q.push(temp);
                   // printf("%d:%d %d %d\n",temp.step,temp.s,temp.n,temp.m);
                }//if
        }//for
    }//while
    return -1;
}
int main(){

    while(scanf("%d%d%d",&s,&n,&m),s){
            if(s%2) {
                    printf("NO\n");
                    continue;
            }//if
            half=s/2;
            st.m=0;st.n=0;st.s=s;st.step=0;
            memset(vis,0,sizeof(vis));

            int ans=bfs();
            if(ans<0) printf("NO\n");
            else printf("%d\n",ans);
    }//while
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值