Metal Steak

Hard to eat

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

公告

aaaaaaaaaaaa

常用链接

留言簿(1)

我参与的团队

搜索

  •  

最新评论

#include <iostream>
using namespace std;

int c = 0, f = 1, r = 0, a = 0, b = 0, prev[10001];

struct stm
{
    
int pota, potb;
    
string cmd;
    stm()
    {
        pota 
= potb = 0;
        cmd 
= "";
    }
    
void addx(stm* q)
    {
        
if(!search(q))
        {
            q[
++r] = *this;
            prev[r] 
= f - 1;
        }
    }
    
bool search(stm* q)
    {
        
for(int i = 1; i <= r; i++)
            
if(q[i] == *this)
                
return true;
        
return false;
    }
    
bool operator == (stm s)
    {
        
if(pota == s.pota && potb == s.potb)
            
return true;
        
else
            
return false;
    }
}que[
10001];

void stmgen(stm s);
void print(stm s, int i);

int main()
{
    memset(prev, 
0sizeof prev);

    stm s;
    
int flag = 0;
    cin 
>> a >> b >> c;
    s.addx(que);
    
for(; ; )
    {
        stmgen(que[f
++]);
        
if(que[r].pota == c || que[r].potb == c)
            
break;
        
if(f == r + 1)
        {
            flag
++;
            
break;
        }
    }
    
    
if(!flag)
    {
        
int cnt = -1;
        
for(int i = r; i != 0; i = prev[i])
            cnt
++;
        cout 
<< cnt << endl;
        print(que[r], r);
    }
    
else
        cout 
<< "impossible" << endl;

    
return 0;
}

void stmgen(stm s)
{
    stm t 
= s;   //Initialize statement.
        t.pota = a;
        t.cmd 
= "FILL(1)";
        t.addx(que);
    t 
= s;   //Initialize statement.
        t.potb = b;
        t.cmd 
= "FILL(2)";
        t.addx(que);
    t 
= s;   //Initialize statement.
        for(; t.pota > 0 && t.potb < b; t.pota--)
            t.potb
++;
        t.cmd 
= "POUR(1,2)";
        t.addx(que);
    t 
= s;   //Initialize statement.
        for(; t.potb > 0 && t.pota < a; t.potb--)
            t.pota
++;
        t.cmd 
= "POUR(2,1)";
        t.addx(que);
    t 
= s;   //Initialize statement.
        t.pota = 0;
        t.cmd 
= "DROP(1)";
        t.addx(que);
    t 
= s;   //Initialize statement.
        t.potb = 0;
        t.cmd 
= "DROP(2)";
        t.addx(que);
}

void print(stm s, int i)
{
    
if(prev[prev[i]])
        print(que[prev[i]], prev[i]);
    cout 
<< s.cmd << endl;
}

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

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