两个空瓶,三种操作
一、把一个瓶子灌满
二、把一个瓶子清空
三、把一个瓶子里面的水灌到另一个瓶子里面去
BFS暴力
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <queue>
#include <algorithm>
#include <iostream>
using namespace std;
struct node
{
int me;
int it;
// int c_vol;
int ope;
int pare;
int step;
};
int vis[105][105];
int flag;
queue <node> qq;
int a,b,c;
node ans[1000000];
int ok=0;
void bfs()
{
node start;
start.me=0;
start.it=0;
start.ope=0;
start.step=0;
qq.push(start);
while(!qq.empty())
{
node t=qq.front();
qq.pop();
if (vis[t.me][t.it]==1)
continue;
else
{
vis[t.me][t.it]=1;
}
if (t.me==c||t.it==c)
{
flag=t.step;
return ;
}
int i;
for (i=1;i<=6;i++)
{
node k=t;
if (i==1&&k.me!=0)
{
k.ope=1; //me空
k.me=0;
}else
if(i==2&&k.it!=0)
{
k.ope=2; // it空
k.it=0;
}else
if (i==3&&k.me<a)
{
k.ope=3; //me满
k.me=a;
}else
if (i==4&&k.it<b)
{
k.ope=4; //it满
k.it=b;
}else
if (i==5&&k.me!=0&&k.it!=b)
{
k.ope=5; //me to it
if (k.it+k.me>=b)
{
k.me-=(b-k.it);
k.it=b;
}
else
{
k.it+=k.me;
k.me=0;
}
}else
if (i==6&&k.it!=0&&k.me!=a)
{
k.ope=6; //it to me
if (k.it+k.me>=a)
{
k.it-=a-k.me;
k.me=a;
}
else
{
k.me+=k.it;
k.it=0;
}
}else
continue;;
k.pare=t.step;
k.step=ok+1;
// if (vis[k.me][k.it]==1)
// continue;
ans[++ok]=k;
qq.push(k);
}
}
}
int put (node tmp)
{
if (tmp.ope==1)
printf("DROP(1)\n");
if (tmp.ope==2)
printf("DROP(2)\n");
if (tmp.ope==3)
printf("FILL(1)\n");
if (tmp.ope==4)
printf("FILL(2)\n");
if (tmp.ope==5)
printf("POUR(1,2)\n");
if (tmp.ope==6)
printf("POUR(2,1)\n");
return 0;
}
int cun=0;
int output(int stp)
{
if (ans[stp].step==0)
{
printf("%d\n",cun);
return 0;
}
else
{
cun++;
output( ans[stp].pare);
put(ans[stp]);
}
}
int main()
{
cin>>a>>b>>c;
flag=0;
memset(vis,0,sizeof(vis));
bfs();
if (!flag)
printf("impossible\n");
else
output(flag );
return 0;
}