H

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <string>
 4 #include <cstring>
 5 #include <queue>
 6 using namespace std;
 7 
 8 const int maxn = 105;
 9 
10 char f[10][10] ={ "FILL(1)","FILL(2)","DROP(1)","DROP(2)","POUR(1,2)","POUR(2,1)"};
11 int a,b,c;
12 struct node{
13     int fx, fy;
14     int step, op;
15 }v[maxn][maxn];
16 
17 struct point{
18     int x, y;
19 }s, q;
20 
21 void dfs(int x, int y){
22     if(x == 0 && y == 0){
23         return ;
24     }
25     dfs(v[x][y].fx,v[x][y].fy);
26 
27     cout << f[v[x][y].op] << endl;
28 }
29 
30 queue<point>p;
31 void bfs(){
32     while(!p.empty()) p.pop();
33     s.x = s.y = 0;
34     v[0][0].step = 1;
35 
36     p.push(s);
37     while(!p.empty()){
38         s = p.front(); p.pop();
39 
40         if(s.x == c || s.y == c){
41             cout << v[s.x][s.y].step - 1 << endl;
42             dfs(s.x, s.y);
43             return ;
44         }
45         for(int i = 0 ;i < 6;i++){
46             q = s;
47             if(i == 0){
48                 q.x = a;
49             }
50             else if(i == 1){
51                 q.y = b;
52             }
53             else if(i == 2){
54                 q.x = 0;
55             }
56             else if(i == 3){
57                 q.y = 0;
58             }
59             else if(i == 4){
60                 if(q.x + q.y <= b){
61                     q.y += q.x, q.x = 0;
62                 }
63                 else
64                     q.x -= (b-q.y), q.y = b;
65             }
66             else if(i == 5){
67                  if(q.x+q.y <= a)
68                     q.x += q.y, q.y = 0;
69                  else
70                     q.y -= (a-q.x), q.x = a;
71             }
72 
73             if(v[q.x][q.y].step == 0){
74                 v[q.x][q.y].step = v[s.x][s.y].step+1;
75                 v[q.x][q.y].fx = s.x;
76                 v[q.x][q.y].fy = s.y;
77                 v[q.x][q.y].op = i;
78 
79                 p.push(q);
80             }
81         }
82     }
83     cout << "impossible" << endl;
84 }
85 
86 int main(){
87     cin >> a >> b >> c;
88         //memset(v,0,sizeof(v));
89         bfs();
90 
91     return 0;
92 }
原文地址:https://www.cnblogs.com/ouyang_wsgwz/p/9004764.html