参考某大牛的算法,还写这么久,真悲剧
#include <iostream>
#include <cstdio>
#include <cstdlib>
using namespace std;
#define Afull 1
#define Bfull 2
#define Aempty 3
#define Bempty 4
#define AtoB 5
#define BtoA 6
int head,tail;
char step[7][10]= {"0","fill A","fill B","empty A","empty B","pour A B","pour B A"};
struct state
{
int mark;
int a;
int b;
struct state *front;
};
state Q[100000];
void enq(state a)
{
Q[head++]=a;
}
state outq()
{
return Q[tail++];
}
int check(state a)
{
for(int i=0; i<head; i++)
{
if(Q[i].a==a.a&&Q[i].b==a.b)
return 1;
}
return 0;
}
int empty()
{
if(head==tail)
return 1;
else
return 0;
}
int main()
{
int A,B,N,i;
state temp,*point,s,*search,*result[100000];
while(scanf("%d%d%d",&A,&B,&N)!=EOF)
{
head=0;
tail=0;
temp.a=0;
temp.b=0;
temp.mark=0;
temp.front=NULL;
enq(temp);
while(!empty())
{
temp=outq();
point=&Q[tail-1];
if(temp.a==N||temp.b==N)
break;
s=temp;
s.a=A;
if(!check(s)&&temp.a!=A)
{
s.front=point;
s.mark=Afull;
enq(s);
}
s=temp;
s.b=B;
if(!check(s)&&temp.b!=B)
{
s.front=point;
s.mark=Bfull;
enq(s);
}
s=temp;
s.a=0;
if(!check(s)&&temp.a)
{
s.front==point;
s.mark=Aempty;
enq(s);
}
s=temp;
s.b=0;
if(!check(s)&&temp.b)
{
s.front=point;
s.mark=Bempty;
enq(s);
}
s=temp;
if(s.a!=0&&s.b<B&&s.a<=B-s.b)
{
s.b+=s.a;
s.a=0;
if(!check(s))
{
s.front=point;
s.mark=AtoB;
enq(s);
}
}
else
{
if(s.a!=0&&s.b<B)
{
s.a-=(B-s.b);
s.b=B;
if(!check(s))
{
s.front=point;
s.mark=AtoB;
enq(s);
}
}
}
if(s.b!=0&&s.a<A&&s.b<=A-s.a)
{
s.a+=s.b;
s.b=0;
if(!check(s))
{
s.front=point;
s.mark=BtoA;
enq(s);
}
}
else
{
if(s.b!=0&&s.a<A)
{
s.a=A;
s.b-=(A-s.b);
if(!check(s))
{
s.front=point;
s.mark=BtoA;
enq(s);
}
}
}
}
search=point;
for(i=0; search!=NULL; i++)
{
result[i]=search;
search=search->front;
}
for(i-=2; i>=0; i--)
puts(step[result[i]->mark]);
printf("success\n");
}
return 0;
}