pojPots

http://poj.org/problem?id=3414

  1 #include<cstdio>
  2 #include<cstring>
  3 #define MAXN 1000000
  4 using namespace std;
  5 
  6 int c,d,e,ans,l;
  7 bool vis[120][120];
  8 struct node
  9 {
 10     int a,b;
 11     int flag;
 12     int pre;
 13     int step;
 14 }p[MAXN],st,st1;
 15 
 16 void bfs()
 17 {
 18     p[0].a=0;
 19     p[0].b=0;
 20     p[0].flag=0;
 21     p[0].step=0;
 22     int s=0,r=1;
 23     vis[0][0]=true;
 24     while(s<r)
 25     {
 26         st=p[s];
 27         if(st.a==e||st.b==e){ans=st.step;l=s; return;}
 28         if(st.a!=c){
 29         st1.a=c; st1.b=st.b; st1.step=st.step+1;
 30         st1.pre=s; st1.flag=1;
 31         if(!vis[st1.a][st1.b]){
 32         vis[st1.a][st1.b]=true;
 33         p[r++]=st1;
 34         }
 35         }
 36         if(st.b!=d){
 37         st1.a=st.a; st1.b=d; st1.step=st.step+1;
 38         st1.pre=s; st1.flag=2;
 39         if(!vis[st1.a][st1.b]){
 40         vis[st1.a][st1.b]=true;
 41         p[r++]=st1;
 42         }
 43         }
 44         if(st.b!=0)
 45         {
 46         if(st.b>=c-st.a){st1.b=st.b-(c-st.a);st1.a=c;}
 47         else  {st1.a=st.a+st.b;st1.b=0;}
 48         st1.step=st.step+1;
 49         st1.pre=s; st1.flag=3;
 50        if(!vis[st1.a][st1.b]){
 51         vis[st1.a][st1.b]=true;
 52         p[r++]=st1;
 53         }
 54         }
 55         if(st.a!=0)
 56         {
 57         if(st.a>=d-st.b){st1.a=st.a-(d-st.b);st1.b=d;}
 58         else  {st1.b=st.b+st.a;st1.a=0;}
 59         st1.step=st.step+1;
 60         st1.pre=s; st1.flag=4;
 61        if(!vis[st1.a][st1.b]){
 62         vis[st1.a][st1.b]=true;
 63         p[r++]=st1;
 64         }
 65         }
 66         if(st.a!=0){
 67         st1.a=0; st1.b=st.b; st1.step=st.step+1;
 68         st1.pre=s; st1.flag=5;
 69         if(!vis[st1.a][st1.b]){
 70         vis[st1.a][st1.b]=true;
 71         p[r++]=st1;
 72         }
 73         }
 74         if(st.b!=0){
 75         st1.a=st.a; st1.b=0; st1.step=st.step+1;
 76         st1.pre=s; st1.flag=6;
 77         if(!vis[st1.a][st1.b]){
 78         vis[st1.a][st1.b]=true;
 79         p[r++]=st1;
 80         }
 81         }
 82         s++;
 83     }
 84 }
 85 
 86 void prin(int k)
 87 {
 88     if(k==0)
 89     {
 90         return ;
 91     }
 92     prin(p[k].pre);
 93     if(p[k].flag==1) printf("FILL(1)
");
 94     else if(p[k].flag==2) printf("FILL(2)
");
 95     else if(p[k].flag==3) printf("POUR(2,1)
");
 96     else if(p[k].flag==4) printf("POUR(1,2)
");
 97     else if(p[k].flag==5) printf("DROP(1)
");
 98     else if(p[k].flag==6) printf("DROP(2)
");
 99 }
100 int main()
101 {
102     while(scanf("%d%d%d",&c,&d,&e)!=EOF){
103         ans=0;
104         memset(vis,false,sizeof(vis));
105     bfs();
106     if(ans!=0){
107     printf("%d
",ans);
108     prin(l);
109     }
110     else printf("impossible
");
111     }
112     return 0;
113 }
View Code
原文地址:https://www.cnblogs.com/fanminghui/p/3290078.html