poj 1606 Jugs

#include <iostream>        //bfs
#include <deque>
#include
<string>
using namespace std;
struct node
{
string path;
int posa,posb;
}ans[
1000000];
bool visited[1005][1005];
string output[6]={"fill A","fill B","empty A","empty B","pour A B","pour B A"};
int main()
{
int ca,cb,n;
while(cin>>ca>>cb>>n)
{
ans[
0].path="";ans[0].posa=ans[0].posb=0;
deque
<node> col;
col.push_back(ans[
0]);
int t=0;
memset(visited,
0,sizeof(visited));
visited[
0][0]=1;
while(!col.empty())
{
node temp
=col.front();
col.pop_front();
int pa=temp.posa,pb=temp.posb;
if(pa==n||pb==n)
{
for(int i=0;i<temp.path.size();++i)
cout
<<output[temp.path[i]-'1']<<endl;
cout
<<"success\n";
break;
}
if(pa<ca&&visited[ca][pb]==0)
{
ans[
++t].path=temp.path+'1';
ans[t].posa
=ca;ans[t].posb=pb;
visited[ca][pb]
=1;
col.push_back(ans[t]);
}
if(pb<cb&&visited[pa][cb]==0)
{
ans[
++t].path=temp.path+'2';
ans[t].posa
=pa;ans[t].posb=cb;
visited[pa][cb]
=1;
col.push_back(ans[t]);
}
if(pa>0&&visited[0][pb]==0)
{
ans[
++t].path=temp.path+'3';
ans[t].posa
=0;ans[t].posb=pb;
visited[
0][pb]=1;
col.push_back(ans[t]);
}
if(pb>0&&visited[pa][0]==0)
{
ans[
++t].path=temp.path+'4';
ans[t].posa
=pa;ans[t].posb=0;
visited[pa][
0]=1;
col.push_back(ans[t]);
}
if(pa>0&&pb<cb)
{
if(pa+pb>cb&&visited[pa-cb+pb][cb]==0)
{
ans[
++t].path=temp.path+'5';
ans[t].posa
=pa-cb+pb;ans[t].posb=cb;
visited[pa
-cb+pb][cb]=1;
col.push_back(ans[t]);
}
if(pa+pb<=cb&&visited[0][pa+pb]==0)
{
ans[
++t].path=temp.path+'5';
ans[t].posa
=0;ans[t].posb=pa+pb;
visited[
0][pa+pb]=1;
col.push_back(ans[t]);
}
}
if(pb>0&&pa<ca)
{
if(pa+pb>ca&&visited[ca][pb-ca+pa]==0)
{
ans[
++t].path=temp.path+'6';
ans[t].posa
=ca;ans[t].posb=pb-ca+pa;
visited[ca][pb
-ca+pa]=1;
col.push_back(ans[t]);
}
if(pa+pb<=ca&&visited[pa+pb][0]==0)
{
ans[
++t].path=temp.path+'6';
ans[t].posa
=pa+pb;ans[t].posb=0;
visited[pa
+pb][0]=1;
col.push_back(ans[t]);
}
}
}
}
return 0;
}

  

posted on 2011-07-22 20:04  sysu_mjc  阅读(129)  评论(0编辑  收藏  举报

导航