POJ - 3414
#include<iostream>
#include<map>
#include<cstring>
#include<queue>
using namespace std;
const int maxn=102;
bool vis[maxn][maxn];
struct node
{
int x, y;
} temp1,temp2;
int a,b,c,cnt,aa[maxn*maxn];
map<int,pair<int,pair<int,int > > >mmp[maxn];
int bfs()
{
queue<node>q;
temp1.x=temp1.y=0;
q.push(temp1);
vis[temp1.x][temp1.y]=1;
while(!q.empty())
{
temp1.x=q.front().x;
temp1.y=q.front().y;
if(temp1.x==c)
{
cnt =temp1.y;
return 1;
}
if(temp1.y==c)
{
cnt =temp1.x;
return 2;
}
q.pop();
if(temp1.x<a&&!vis[a][temp1.y])
{
temp2.x=a;
temp2.y=temp1.y;
vis[temp2.x][temp2.y]=1;
q.push(temp2);
mmp[temp2.x][temp2.y]=make_pair(1,make_pair(temp1.x,temp1.y));
}
if(temp1.y<b&&!vis[temp1.x][b])
{
temp2.x=temp1.x;
temp2.y=b;
q.push(temp2);
vis[temp2.x][temp2.y]=1;
mmp[temp2.x][temp2.y]=make_pair(2,make_pair(temp1.x,temp1.y));
}
if(temp1.x>0&&!vis[0][temp1.y])
{
temp2.x=0;
temp2.y=temp1.y;
q.push(temp2);
vis[temp2.x][temp2.y]=1;
mmp[temp2.x][temp2.y]=make_pair(3,make_pair(temp1.x,temp1.y));
}
if(temp1.y>0&&!vis[temp1.x][0])
{
temp2.x=temp1.x;
temp2.y=0;
q.push(temp2);
vis[temp2.x][temp2.y]=1;
mmp[temp2.x][temp2.y]=make_pair(4,make_pair(temp1.x,temp1.y));
}
if(temp1.x<a&&!vis[temp1.x+min(a-temp1.x,temp1.y)][temp1.y-min(a-temp1.x,temp1.y)])
{
temp2.x=temp1.x+min(a-temp1.x,temp1.y);
temp2.y=temp1.y-min(a-temp1.x,temp1.y);
q.push(temp2);
vis[temp2.x][temp2.y]=1;
mmp[temp2.x][temp2.y]=make_pair(5,make_pair(temp1.x,temp1.y));
}
if(temp1.y<b&&!vis[temp1.x-min(b-temp1.y,temp1.x)][temp1.y+min(b-temp1.y,temp1.x)])
{
temp2.x=temp1.x-min(b-temp1.y,temp1.x);
temp2.y=temp1.y+min(b-temp1.y,temp1.x);
q.push(temp2);
vis[temp2.x][temp2.y]=1;
mmp[temp2.x][temp2.y]=make_pair(6,make_pair(temp1.x,temp1.y));
}
}
return -1;
}
int main()
{
cin>>a>>b>>c;
memset(vis,0,sizeof(vis));
int ans=bfs(),sum=0;
if(ans==-1)
cout<<"impossible"<<endl;
else
{
int tx,ty;
if(ans==1)
{
tx=c;
ty=cnt;
}
if(ans==2)
{
ty=c;
tx=cnt;
}
while(1)
{
if(tx==0&&ty==0)
break;
aa[sum++]=mmp[tx][ty].first;
int tempx=mmp[tx][ty].second.first;
int tempy=mmp[tx][ty].second.second;
tx=tempx;
ty=tempy;
}
cout<<sum<<endl;
for(int i=sum-1; i>=0; i--)
{
if(aa[i]==2)
cout<<"FILL(2)"<<endl;
if(aa[i]==1)
cout<<"FILL(1)"<<endl;
if(aa[i]==3)
cout<<"DROP(1)"<<endl;
if(aa[i]==4)
cout<<"DROP(2)"<<endl;
if(aa[i]==5)
cout<<"POUR(2,1)"<<endl;
if(aa[i]==6)
cout<<"POUR(1,2)"<<endl;
}
}
return 0;
}