题目好像是模拟水桶来回倒的问题,full就是倒满,drop就是全倒掉,pour就是从一个通去另一个桶,今天poj还是502.。。也不知道写的对不对,但样例过了。。嗯,就那一个样例Orz。。。。模拟一下暴力解决就行了,不过回头改。。。
警告!!!没有A!!!虽然我找了半天没找到错在哪里。。
#include<iostream>
#include<cstring>
#include<queue>
#include<stack>
using namespace std;
struct pot
{
int x1,x2,time,pre,location;string op;
void init(int xx1,int xx2,int t,int p,int l,string o)
{
x1=xx1;x2=xx2;time=t;pre=p;location=l;op=o;
}
};
pot q[10005];
bool book[105][105];
int a,b,c,head=0,tail=0;
void push(pot temp)
{
q[tail++]=temp;
}
pot pop()
{
pot temp=q[head++];
return temp;
}
int bfs()
{
pot t;
t.init(0,0,0,-1,0,"null");
book[t.x1][t.x2]=1;
push(t);
while(head!=tail)
{
pot temp=pop();
if(temp.x1==c||temp.x2==c)return temp.location;
//full 1
if(!book[a][temp.x2])
{
pot p;p.init(a,temp.x2,temp.time+1,temp.location,tail,"FILL(1)");
book[a][temp.x2]=1;
push(p);
}
//full 2
if(!book[temp.x1][b])
{
pot p;p.init(temp.x1,b,temp.time+1,temp.location,tail,"FILL(2)");
book[temp.x1][b]=1;
push(p);
}
//drop 1
if(!book[0][temp.x2])
{
book[0][temp.x2];
pot p;p.init(0,temp.x2,temp.time+1,temp.location,tail,"DROP(1)");
push(p);
}
//drop 2
if(!book[temp.x1][0])
{
book[temp.x1][0]=1;
pot p;p.init(temp.x1,0,temp.time+1,temp.location,tail,"DROP(1)");
push(p);
}
//pour 2-1
int r1=a-temp.x1;
if(r1>temp.x2)//可以全部装进去
{
if(!book[temp.x1+temp.x2][0])
{
book[temp.x1+temp.x2][0]=1;
pot p;p.init(temp.x1+temp.x2,0,temp.time+1,temp.location,tail,"POUR(2,1)");
push(p);
}
}
else if(r1>0)
{
if(!book[a][temp.x2-r1])
{
book[a][temp.x2-r1]=1;
pot p;p.init(a,temp.x2-r1,temp.time+1,temp.location,tail,"POUR(2,1)");
push(p);
}
}
int r2=b-temp.x2;
if(r2>temp.x1)//可以全部装进去
{
if(!book[0][temp.x2+temp.x1])
{
book[0][temp.x2+temp.x1]=1;
pot p;p.init(0,temp.x2+temp.x1,temp.time+1,temp.location,tail,"POUR(1,2)");
push(p);
}
}
else if(r2>0)
{
if(!book[temp.x1-r2][b])
{
book[temp.x1-r2][b]=1;
pot p;p.init(temp.x1-r2,b,temp.time+1,temp.location,tail,"POUR(1,2)");
push(p);
}
}
}
return -1;
}
void print()
{
int k=bfs();
if(k<0)cout<<"Impossible"<<endl;
else
{
cout<<q[k].time<<endl;
stack<string>s;
while(k!=0)
{
string temp=q[k].op;
s.push(temp);
k=q[k].pre;
}
while(!s.empty())
{
string temp=s.top();
cout<<temp<<endl;
s.pop();
}
}
}
int main()
{
cin>>a>>b>>c;
print();
return 0;
}