Metal Steak

Hard to eat

  C++博客 :: 首页 :: 联系 :: 聚合  :: 管理
  0 Posts :: 79 Stories :: 0 Comments :: 0 Trackbacks

公告

aaaaaaaaaaaa

常用链接

留言簿(1)

我参与的团队

搜索

  •  

最新评论

五中搬新校区了,看到五中同学们刻苦学习的镜头,学校领导很是感动,决定 在五中食堂为五中学子们免费提供一款可口可乐公司最新研发的饮料(据说这种饮料能提高记忆力,改善大脑的疲劳,赛过金思力),呵呵,当然这种饮料不能多 喝,也并不是人人都能享用的。只有爱动脑筋的人才能喝得了。
学校提供三个没有刻度的酒杯a,b,c。已知a的容量为10毫升,b的容量为7毫升,c的容量为3毫升。初始状态为a中装满饮料,b和c为空 杯。问题是给出某一时刻提供饮料L毫升后,请你利用b和c杯,用最少的倒饮料次数,在a杯中倒出L毫升的饮料。只有完成这个任务后,你才能享用这L毫升的 饮料。


#include 
<iostream>
using namespace std;

const int
= 10, b = 7, c = 3;
int
= 1, r = 0, prev[1001], target, flag = false;

void
__outp__( 
int x );

struct
status
{
    
string cmd;
    
int
    a, b, c;
    status()
    {
        a 
= b = c = 0;
    }
    
void
    addx( status 
*q )
    {

        
if!search( q ) )
        {
            q[
++r] = *this;
            prev[r] 
= f - 1;

        }
        
ifthis->== target )
        {
            __outp__( r );
            exit(
0);
        }
    }
    
bool
    search( status 
*q )
    {
        
forint i = 1; i <= r; i++ )
            
if( q[i] == *this )
                
return true;
        
return false;
    }
    
bool
    
operator == ( status s )
    {
        
if( a == s.a && b == s.b && c == s.c )
            
return true;
        
return false;
    }
}que[
1001];

void
__read__()
{
    cin 
>> target;
}

void
__bfs__( status s )
{
    status st 
= s;
    
for( ; st.a > 0 && st.b < b; st.a-- )
        st.b
++;
    st.cmd 
= "a->b";
    st.addx( que );

    st 
= s;
    
for( ; st.a > 0 && st.c < c; st.a-- )
        st.c
++;
    st.cmd 
= "a->c";
    st.addx( que );

    st 
= s;
    
for( ; st.b > 0 && st.c < c; st.b-- )
        st.c
++;
    st.cmd 
= "b->c";
    st.addx( que );

    st 
= s;
    
for( ; st.b > 0 && st.a < a; st.b-- )
        st.a
++;
    st.cmd 
= "b->a";
    st.addx( que );

    st 
= s;
    
for( ; st.c > 0 && st.a < a; st.c-- )
        st.a
++;
    st.cmd 
= "c->a";
    st.addx( que );

    st 
= s;
    
for( ; st.c > 0 && st.b < b; st.c-- )
        st.b
++;
    st.cmd 
= "c->b";
    st.addx( que );
}

void
__outp__( 
int x )
{
    
if( prev[x] != 1 )
        __outp__( prev[x] );
    cout 
<< que[x].cmd << endl;
}

int
main()
{
    status source;
    source.a 
= a;
    source.addx( que );
    __read__();
    
if( target == 0 )
    {
        cout 
<< "a->b" << endl << "a->c" << endl;
        
return 0;
    }
    
while( f != r + 1 )
        __bfs__( que[f
++] );
    cout 
<< "no answer!" << endl;

    
return 0;
}

posted on 2009-09-15 21:25 mad4alcohol 阅读(98) 评论(0)  编辑 收藏 引用

只有注册用户登录后才能发表评论。
网站导航:   博客园   博客园最新博文   博问   管理