hdu 1495 非常可乐(简单的的bfs)


代码有错误 ,过不了,肯跌 还没检查出来委屈

#include<iostream>
#include<cstdio>
#include<memory.h>
#include<queue>
using namespace std;
struct node
{
    int bottle[3];
    int t;
};
int N,M,S;
int vis[101][101][101];
int main()
{
    while(scanf("%d %d %d",&S,&N,&M)!=EOF)
    {
        if(N+M+S==0)
            break;
        if(S%2==1)
        {
            printf("NO\n");
            continue;
        }
        queue<node>q;
        node start,st;
        start.bottle[0]=S,start.bottle[1]=0,start.bottle[2]=0,start.t=0;
        st.bottle[0]=S,st.bottle[1]=N,st.bottle[2]=M;
        int ans=0,flag=0,half=S/2;
        memset(vis,0,sizeof(vis));
        vis[S][0][0]=1;
        while(!q.empty())
            q.pop();
        q.push(start);
        while(!q.empty())
        {
            node cur=q.front();
            q.pop();
            if((cur.bottle[0]==half&&cur.bottle[1]==half)||
               (cur.bottle[0]==half&&cur.bottle[2]==half)||
               (cur.bottle[1]==half&&cur.bottle[2]==half))
            {
                ans=cur.t;
                flag=1;
                break;
            }
           /* node temp=cur;
            temp.t++;*/
            cur.t++;
            for(int i=0;i<3;i++)
            {
                if(cur.bottle[i]>0)
                {
                    for(int j=0;j<3;j++)
                    {
                        if(i!=j)
                        {
                            node temp=cur;/**放错位置了*/
                            if(temp.bottle[i]<=st.bottle[j]-temp.bottle[j])
                            {
                                temp.bottle[j]+=temp.bottle[i];
                                temp.bottle[i]=0;
                            }
                            else
                            {
                                //temp.bottle[i]-=st.bottle[j]-temp.bottle[j];
                                //temp.bottle[i]=temp.bottle[i]-(st.bottle[j]-temp.bottle[j]);
                                temp.bottle[i]=temp.bottle[i]+temp.bottle[j]-st.bottle[j];
                                temp.bottle[j]=st.bottle[j];
                            }

                            /**if(temp.bottle[i]+temp.bottle[j]>st.bottle[j])
                            {
                                temp.bottle[j]=st.bottle[j];
                                //temp.bottle[i]=temp.bottle[i]+temp.bottle[j]-st.bottle[j];
                                temp.bottle[i]=temp.bottle[i]-(st.bottle[j]-temp.bottle[j]);
                            }
                            else
                            {
                                temp.bottle[j]+=temp.bottle[i];
                                temp.bottle[i]=0;
                            }*/
                            if(vis[temp.bottle[0]][temp.bottle[1]][temp.bottle[2]]==0)
                            {
                                q.push(temp);
                                //printf("%d %d %d    ",temp.bottle[0],temp.bottle[1],temp.bottle[2]);
                                vis[temp.bottle[0]][temp.bottle[1]][temp.bottle[2]]=1;
                            }
                        }
                    }
                }
            }
        }
        if(flag==0)
            printf("NO\n");
        else
            printf("%d\n",ans);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值