POJ 3414

照着打的。学到了学到了。果然,太菜了。

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cmath>
 4 #include <cstring>
 5 #include <algorithm>
 6 #include <queue>
 7 #include <stack>
 8 using namespace std;
 9 struct node
10 {
11     int a,b,step,flag;
12     node *pre;
13 };
14 int n,m,c,vis[117][117]={0},flag;
15 stack<int>r;
16 queue<node>q;
17 void bfs()
18 {
19     node s,temp[400],next;
20     int count=-1;
21     s.a=0;s.b=0;s.step=0;s.pre=NULL;s.flag=0;
22     vis[s.a][s.b]=1;
23     q.push(s);
24     while(!q.empty())
25     {
26         count++;
27         temp[count]=q.front();q.pop();
28         for(int i=1;i<=6;i++)
29         {
30             switch(i)
31             {
32                 case 1:next.a=n;next.b=temp[count].b;next.flag=1;break;
33                 case 2:next.a=temp[count].a;next.b=m;next.flag=2;break;
34                 case 3:next.a=0;next.b=temp[count].b;next.flag=3;break;
35                 case 4:next.a=temp[count].a;next.b=0;next.flag=4;break;
36                 case 5:if(m>=temp[count].a+temp[count].b){next.a=0;next.b=temp[count].b+temp[count].a;}
37                        else{next.a=temp[count].a-(m-temp[count].b);next.b=m;}next.flag=5;break;
38                 case 6:if(n>=temp[count].b+temp[count].a){next.b=0;next.a=temp[count].b+temp[count].a;}
39                        else{next.b=temp[count].b-(n-temp[count].a);next.a=n;}next.flag=6;break;
40             }
41             if(vis[next.a][next.b]==0)
42             {
43                 vis[next.a][next.b]=1;
44                 next.step=temp[count].step+1;
45                 next.pre=&temp[count];
46                 if(next.a==c||next.b==c)
47                 {
48                     flag=next.step;
49                     while(next.pre)
50                     {
51                         r.push(next.flag);
52                         next=*next.pre;
53                     }
54                     return;
55                 }
56                 q.push(next);
57             }
58         }
59     }
60 }
61 void print()
62 {
63     int i;
64     while(!r.empty())
65     {
66         i=r.top();r.pop();
67         switch(i)
68         {
69             case 1:printf("FILL(1)\n");break;
70             case 2:printf("FILL(2)\n");break;
71             case 3:printf("DROP(1)\n");break;
72             case 4:printf("DROP(2)\n");break;
73             case 5:printf("POUR(1,2)\n");break;
74             case 6:printf("POUR(2,1)\n");break;
75         } 
76     }
77 }
78 int main(int argc, char *argv[])
79 {
80     
81     scanf("%d%d%d",&n,&m,&c);
82     flag=0;
83     bfs();
84     if(flag==0)
85     printf("impossible\n");
86     else
87     {
88         printf("%d\n",flag);
89         print();
90     }
91 }

 

posted @ 2018-07-25 21:39  huluxin  阅读(96)  评论(0编辑  收藏  举报