poj 3414 Pots

#include <iostream>        //BFS
#include <string>
#include
<deque>
using namespace std;
struct Node
{
int f1,f2;
string path;
Node()
{
f1
=f2=0;
path
="";
}
}node[
10000];
int t,visited[105][105];
int main()
{
int a,b,c,e1,e2;
string str;
cin
>>a>>b>>c;
deque
<Node> col;
col.push_back(node[
0]);
while(!col.empty())
{
Node temp
=col.front();
col.pop_front();
e1
=temp.f1;e2=temp.f2;
if(e1==c||e2==c)
{
cout
<<temp.path.size()<<endl;
for(int i=0;i<temp.path.size();++i)
{
char ch=temp.path[i];
if(ch=='1')
cout
<<"FILL(1)\n";
else if(ch=='2')
cout
<<"FILL(2)\n";
else if(ch=='3')
cout
<<"DROP(1)\n";
else if(ch=='4')
cout
<<"DROP(2)\n";
else if(ch=='5')
cout
<<"POUR(1,2)\n";
else if(ch=='6')
cout
<<"POUR(2,1)\n";
}
return 0;
}
if(visited[e1][e2])
continue;
visited[e1][e2]
=1;
if(e1<a)
{
node[
++t].f1=a;node[t].f2=e2;
node[t].path
=temp.path+"1";
col.push_back(node[t]);
}
if(e2<b)
{
node[
++t].f1=e1;node[t].f2=b;
node[t].path
=temp.path+"2";
col.push_back(node[t]);
}
if(e1>0)
{
node[
++t].f1=0;node[t].f2=e2;
node[t].path
=temp.path+"3";
col.push_back(node[t]);
}
if(e2>0)
{
node[
++t].f1=e1;node[t].f2=0;
node[t].path
=temp.path+"4";
col.push_back(node[t]);
}
if(e1>0&&e2<b)
{
if(e1>b-e2)
{
node[
++t].f1=e1-b+e2;
node[t].f2
=b;
node[t].path
=temp.path+"5";
col.push_back(node[t]);
}
else
{
node[t].f1
=0;
node[t].f2
=e1+e2;
node[t].path
=temp.path+"5";
col.push_back(node[t]);
}
}
if(e1<a&&e2>0)
{
if(e2>a-e1)
{
node[
++t].f1=a;
node[t].f2
=e2-a+e1;
node[t].path
=temp.path+"6";
col.push_back(node[t]);
}
else
{
node[
++t].f1=e1+e2;
node[t].f2
=0;
node[t].path
=temp.path+"6";
col.push_back(node[t]);
}
}
}
cout
<<"impossible\n";
return 0;
}

  

posted on 2011-07-22 19:56  sysu_mjc  阅读(117)  评论(0编辑  收藏  举报

导航