zoukankan      html  css  js  c++  java
  • BFS 加记录路径

    编程解决如下数学问题:有12升水,怎样利用一个8升和一个5升的容器将水分为两个6升?要求以如下格式打印出分水步骤。(20分)

       a12 b8 c5

       12   0    0

       *    *    * ( “*”表示当前状态下每个容器的盛水量)

       ......

       0    6   6

    View Code
    #include<iostream>
    #include<queue>
    #include<stack>
    using namespace std;

    struct data1
    {
    int x,y,z;
    int use;
    }hash[19][19][19];

    int w,ll,rr;
    struct data
    {
    int a,b,c;
    };

    int bfs()
    {
    int temp;
    queue<data>q;
    data f,s,t;
    f.a=12;f.b=0;f.c=0;

    q.push(f);
    while(!q.empty())
    {
    s=q.front();
    q.pop();

    //a->b
    t=s;
    if(t.a!=0)
    {
    if(t.b!=ll)
    {
    temp=ll-t.b;
    if(temp<t.a)
    {
    t.b=ll;
    t.a-=temp;
    }
    else
    {
    t.b+=t.a;
    t.a=0;
    }
    }

    if(hash[t.a][t.b][t.c].use==-1)
    {
    q.push(t);
    hash[t.a][t.b][t.c].use=1;
    hash[t.a][t.b][t.c].x=s.a;
    hash[t.a][t.b][t.c].y=s.b;
    hash[t.a][t.b][t.c].z=s.c;
    }if(t.a==6&&t.b==6) return 1;}

    //b->a
    t=s;
    if(t.b!=0)
    {
    t.a+=t.b;
    t.b=0;

    if(hash[t.a][t.b][t.c].use==-1)
    {
    q.push(t);
    hash[t.a][t.b][t.c].use=1;
    hash[t.a][t.b][t.c].x=s.a;
    hash[t.a][t.b][t.c].y=s.b;
    hash[t.a][t.b][t.c].z=s.c;
    }if(t.a==6&&t.b==6) return 1;
    }

    //a->c
    t=s;
    if(t.a!=0)
    {
    if(t.c!=rr)//a->c
    {
    temp=rr-t.c;
    if(temp<t.a)
    {
    t.c=rr;
    t.a-=temp;
    }
    else
    {
    t.c+=t.a;
    t.a=0;
    }
    }

    if(hash[t.a][t.b][t.c].use==-1)
    {
    q.push(t);
    hash[t.a][t.b][t.c].use=1;
    hash[t.a][t.b][t.c].x=s.a;
    hash[t.a][t.b][t.c].y=s.b;
    hash[t.a][t.b][t.c].z=s.c;
    }if(t.a==6&&t.b==6) return 1;}


    //c->a
    t=s;
    if(t.c!=0)
    {
    t.a+=t.c;
    t.c=0;

    if(hash[t.a][t.b][t.c].use==-1)
    {
    q.push(t);
    hash[t.a][t.b][t.c].use=1;
    hash[t.a][t.b][t.c].x=s.a;
    hash[t.a][t.b][t.c].y=s.b;
    hash[t.a][t.b][t.c].z=s.c;
    }if(t.a==6&&t.b==6) return 1;}


    //b->c
    t=s;
    if(t.b!=0)
    {
    if(t.c!=rr)//a->c
    {
    temp=rr-t.c;
    if(temp<t.b)
    {
    t.c=rr;
    t.b-=temp;
    }
    else
    {
    t.c+=t.b;
    t.b=0;
    }
    }

    if(hash[t.a][t.b][t.c].use==-1)
    {
    q.push(t);
    hash[t.a][t.b][t.c].use=1;
    hash[t.a][t.b][t.c].x=s.a;
    hash[t.a][t.b][t.c].y=s.b;
    hash[t.a][t.b][t.c].z=s.c;
    }if(t.a==6&&t.b==6) return 1;}


    //c->b
    t=s;
    if(t.c!=0)
    {
    if(t.b!=ll)//a->c
    {
    temp=ll-t.b;
    if(temp<t.c)
    {
    t.b=ll;
    t.c-=temp;
    }
    else
    {
    t.b+=t.c;
    t.c=0;
    }
    }

    if(hash[t.a][t.b][t.c].use==-1)
    {
    q.push(t);
    hash[t.a][t.b][t.c].use=1;
    hash[t.a][t.b][t.c].x=s.a;
    hash[t.a][t.b][t.c].y=s.b;
    hash[t.a][t.b][t.c].z=s.c;
    }if(t.a==6&&t.b==6) return 1;}


    }

    printf("bu xing");
    return 0;

    }


    int main()
    {
    // while(scanf("%d%d%d",&w,&ll,&rr)!=EOF)
    {
    ll=8;
    rr=5;

    memset(hash,-1,sizeof(hash));

    hash[12][0][0].use=1;

    if(bfs()==1)
    printf("ke\n");

    int a=6,b=6,c=0;
    data temp;
    stack<data> st;
    while(hash[a][b][c].use==1)
    {
    //printf("%d %d %d\n",a,b,c);
    int ta=a,tb=b,tc=c;
    a=hash[ta][tb][tc].x;
    b=hash[ta][tb][tc].y;
    c=hash[ta][tb][tc].z;
    temp.a=a;temp.b=b;temp.c=c;
    st.push(temp);
    }

    st.pop();
    while(!st.empty())
    {
    temp=st.top();
    st.pop();

    printf("%d %d %d\n",temp.a,temp.b,temp.c);
    }
    printf("6 6 0\n");
    }
    }



  • 相关阅读:
    bzoj 2120 数颜色 带修改莫队
    luogu 2709 小B的询问 莫队
    bzoj 2002 [Hnoi2010]Bounce 弹飞绵羊 分块
    bzoj 4765 普通计算姬 dfs序 + 分块
    loj 数列分块入门 6 9(区间众数)
    loj 数列分块入门 5 7 8
    AtCoder Grand Contest 021 D
    Codeforces Round #466
    office 威胁检测
    修改macos的启动LOGO
  • 原文地址:https://www.cnblogs.com/huhuuu/p/2228917.html
Copyright © 2011-2022 走看看