1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
| #include <iostream> #include <queue> #include <string> #include <cstring> #include <cstdio>
using namespace std;
#define max 105
struct pot { int a, b; string str; pot() = default; pot(int a, int b, string str) : a(a), b(b), str(str) {} }; int a, b, c; queue<pot> q; bool vis[max][max]; string op[6] = {"FILL(1)", "FILL(2)", "DROP(1)", "DROP(2)", "POUR(1,2)", "POUR(2,1)"}; string order[6] = {"0", "1", "2", "3", "4", "5"};
bool bfs() { memset(vis, false, sizeof(vis)); q.push(pot(0, 0, "")); while(!q.empty()) { pot t = q.front(); q.pop();
if(vis[t.a][t.b]) continue; vis[t.a][t.b] = true;
if(t.a == c || t.b == c) { int len = t.str.size(); printf("%d\n", len); for(int i = 0;i < len; ++i) { printf("%s\n", op[t.str[i] - '0'].c_str()); } return true; }
for(int i = 0;i < 6; ++i) { pot op = t;
switch(i) { case 0 : op.a = a; break; case 1: op.b = b; break; case 2: op.a = 0; break; case 3: op.b = 0; break; case 4: if(op.a + op.b >= b){ op.b = b; op.a = t.a + t.b - b;} else {op.b = t.a + t.b; op.a = 0;} break; case 5: if(op.a + op.b >= a) { op.a = a; op.b = t.a + t.b - a;} else {op.a = t.a + t.b; op.b = 0;} break; default: break; } op.str = t.str + order[i]; q.push(op); } } return false; }
int main() { while(~scanf("%d%d%d",&a, &b, &c)) { bool flag = bfs(); if(!flag) printf("impossible\n"); } return 0; }
|