思路倒是挺简单的,就是以壶的容量为状态,进行宽度搜索,并对结果进行适当的标记,不过代码实现起来实在是太繁琐了。。。。六种情况逐一列举。。。anyway,还是轻松AC了。
#include<iostream>
#include<queue>
#include<stack>
using namespace std;
struct Node{
bool checked;
int action;
int x,y;
int step;
int box1,box2;
};
queue<Node> q;
stack<Node> s;
Node node[101][101];
int a,b,c;
int main(){
Node tmp;
int tmpbox1,tmpbox2;
cin>>a>>b>>c;
while(q.empty()==false)
q.pop();
while(s.empty()==false)
s.pop();
for(int i=0;i<101;i++)
for(int j=0;j<101;j++){
node[i][j].checked=false;
node[i][j].box1=i;
node[i][j].box2=j;
//node[i][j].step=999999;
}
node[0][0].checked=true;
node[0][0].step=0;
q.push(node[0][0]);
while(q.empty()==false){
tmp=q.front();
if(tmp.box1==c||tmp.box2==c)
break;
tmpbox1=tmp.box1;
tmpbox2=tmp.box2;
q.pop();
//DROP(2)
if(node[tmpbox1][0].checked==false){
node[tmpbox1][0].checked=true;
node[tmpbox1][0].step=tmp.step+1;
node[tmpbox1][0].x=tmp.box1;
node[tmpbox1][0].action=1;
node[tmpbox1][0].y=tmp.box2;
q.push(node[tmpbox1][0]);
}
//FILL(2)
if(node[tmpbox1][b].checked==false){
node[tmpbox1][b].checked=true;
node[tmpbox1][b].step=tmp.step+1;
node[tmpbox1][b].x=tmp.box1;
node[tmpbox1][b].action=2;
node[tmpbox1][b].y=tmp.box2;
q.push(node[tmpbox1][b]);
}
//DROP(1)
if(node[0][tmpbox2].checked==false){
node[0][tmpbox2].checked=true;
node[0][tmpbox2].step=tmp.step+1;
node[0][tmpbox2].x=tmp.box1;
node[0][tmpbox2].action=3;
node[0][tmpbox2].y=tmp.box2;
q.push(node[0][tmpbox2]);
}
//FILL(1)
if(node[a][tmpbox2].checked==false){
node[a][tmpbox2].checked=true;
node[a][tmpbox2].step=tmp.step+1;
node[a][tmpbox2].x=tmp.box1;
node[a][tmpbox2].action=4;
node[a][tmpbox2].y=tmp.box2;
q.push(node[a][tmpbox2]);
}
//POUR(1,2)
if(tmpbox1<=(b-tmpbox2)&&node[0][tmpbox1+tmpbox2].checked==false){
node[0][tmpbox1+tmpbox2].checked=true;
node[0][tmpbox1+tmpbox2].step=tmp.step+1;
node[0][tmpbox1+tmpbox2].x=tmp.box1;
node[0][tmpbox1+tmpbox2].action=5;
node[0][tmpbox1+tmpbox2].y=tmp.box2;
q.push(node[0][tmpbox1+tmpbox2]);
}
//POUR(1,2)
else if(tmpbox1>(b-tmpbox2)&&node[tmpbox1-(b-tmpbox2)][b].checked==false){
node[tmpbox1-(b-tmpbox2)][b].checked=true;
node[tmpbox1-(b-tmpbox2)][b].step=tmp.step+1;
node[tmpbox1-(b-tmpbox2)][b].x=tmp.box1;
node[tmpbox1-(b-tmpbox2)][b].action=5;
node[tmpbox1-(b-tmpbox2)][b].y=tmp.box2;
q.push(node[tmpbox1-(b-tmpbox2)][b]);
}
//POUR(2,1)
if(tmpbox2<=(a-tmpbox1)&&node[tmpbox1+tmpbox2][0].checked==false){
node[tmpbox1+tmpbox2][0].checked=true;
node[tmpbox1+tmpbox2][0].step=tmp.step+1;
node[tmpbox1+tmpbox2][0].x=tmp.box1;
node[tmpbox1+tmpbox2][0].action=6;
node[tmpbox1+tmpbox2][0].y=tmp.box2;
q.push(node[tmpbox1+tmpbox2][0]);
}
//POUR(2,1)
else if(tmpbox2>(a-tmpbox1)&&node[a][tmpbox2-(a-tmpbox1)].checked==false){
node[a][tmpbox2-(a-tmpbox1)].checked=true;
node[a][tmpbox2-(a-tmpbox1)].step=tmp.step+1;
node[a][tmpbox2-(a-tmpbox1)].x=tmp.box1;
node[a][tmpbox2-(a-tmpbox1)].action=6;
node[a][tmpbox2-(a-tmpbox1)].y=tmp.box2;
q.push(node[a][tmpbox2-(a-tmpbox1)]);
}
}
if(q.empty()==true){
cout<<"impossible"<<endl;
return 0;
}
cout<<tmp.step<<endl;
//s.push(tmp);
while(1){
s.push(tmp);
if(tmp.x==0&&tmp.y==0)
break;
tmp=node[tmp.x][tmp.y];
}
while(s.empty()==false){
tmp=s.top();
s.pop();
if(tmp.action==1)
cout<<"DROP(2)"<<endl;
else if(tmp.action==2)
cout<<"FILL(2)"<<endl;
else if(tmp.action==3)
cout<<"DROP(1)"<<endl;
else if(tmp.action==4)
cout<<"FILL(1)"<<endl;
else if(tmp.action==5)
cout<<"POUR(1,2)"<<endl;
else if(tmp.action==6)
cout<<"POUR(2,1)"<<endl;
}
//system("pause");
return 0;
}