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

来源:互联网 发布:电脑麦克风调音软件 编辑:程序博客网 时间:2024/05/16 08:08


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

#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;}


原创粉丝点击