Pots (POJ - 3414)
题面
You are given two pots, having the volume of A and B liters respectively. The following operations can be performed:
- FILL(i) fill the pot i (1 ≤ i ≤ 2) from the tap;
- DROP(i) empty the pot i to the drain;
- POUR(i,j) pour from pot i to pot j; after this operation either the pot j is full (and there may be some water left in the pot i), or the pot i is empty (and all its contents have been moved to the pot j).
Write a program to find the shortest possible sequence of these operations that will yield exactly C liters of water in one of the pots.
输入
On the first and only line are the numbers A, B, and C. These are all integers in the range from 1 to 100 and C≤max(A,B).
输出
The first line of the output must contain the length of the sequence of operations K. The following K lines must each describe one operation. If there are several sequences of minimal length, output any one of them. If the desired result can’t be achieved, the first and only line of the file must contain the word ‘impossible’.
样例输入
13 5 4
样例输出
16
2FILL(2)
3POUR(2,1)
4DROP(1)
5POUR(2,1)
6FILL(2)
7POUR(2,1)
提示
无
思路
倒水问题,bfs+路径输出
代码
1using namespace std;
2typedef long long ll;
3const int inf = 0x3f3f3f3f;
4const int N = 1e2+5;
5
6bool vis[N][N] = {{0}};
7int A, B, C;
8
9const char* path[] = {
10 "FILL(1)",
11 "FILL(2)",
12 "DROP(1)",
13 "DROP(2)",
14 "POUR(1,2)",
15 "POUR(2,1)"
16};
17
18struct P {
19 int a, b;
20 int p[N];
21 int n;
22};
23
24P bfs() {
25 memset(vis, 0, sizeof(vis));
26
27 P sp;
28 sp.a = 0;
29 sp.b = 0;
30 sp.n = 0;
31 memset(sp.p, 0, sizeof(sp.p));
32
33 queue<P> q;
34 q.push(sp);
35 vis[sp.a][sp.b] = 1;
36
37 while(!q.empty()) {
38 P tp = q.front();
39 q.pop();
40
41 if(tp.a==C || tp.b==C) {
42 return tp;
43 }
44
45 P np = tp;
46 np.n = tp.n+1;
47
48 if(tp.a<A) {
49 np.a = A;
50 np.b = tp.b;
51 if(!vis[np.a][np.b]) {
52 np.p[tp.n] = 0;
53 q.push(np);
54 vis[np.a][np.b] = 1;
55 }
56 }
57
58 if(tp.b<B) {
59 np.a = tp.a;
60 np.b = B;
61 if(!vis[np.a][np.b]) {
62 np.p[tp.n] = 1;
63 q.push(np);
64 vis[np.a][np.b] = 1;
65 }
66 }
67
68 if(tp.a) {
69 np.a = 0;
70 np.b = tp.b;
71 if(!vis[np.a][np.b]) {
72 np.p[tp.n] = 2;
73 q.push(np);
74 vis[np.a][np.b] = 1;
75 }
76 }
77
78 if(tp.b) {
79 np.a = tp.a;
80 np.b = 0;
81 if(!vis[np.a][np.b]) {
82 np.p[tp.n] = 3;
83 q.push(np);
84 vis[np.a][np.b] = 1;
85 }
86 }
87
88 if(tp.a && tp.b<B) {
89 if(tp.a > B-tp.b) {
90 np.a = tp.a-B+tp.b;
91 np.b = B;
92 } else {
93 np.a = 0;
94 np.b = tp.a+tp.b;
95 }
96 if(!vis[np.a][np.b]) {
97 np.p[tp.n] = 4;
98 q.push(np);
99 vis[np.a][np.b] = 1;
100 }
101 }
102
103 if(tp.b && tp.a<A) {
104 if(tp.b > A-tp.a) {
105 np.a = A;
106 np.b = tp.b-A+tp.a;
107 } else {
108 np.a = tp.a+tp.b;
109 np.b = 0;
110 }
111 if(!vis[np.a][np.b]) {
112 np.p[tp.n] = 5;
113 q.push(np);
114 vis[np.a][np.b] = 1;
115 }
116 }
117 }
118 return sp;
119}
120
121int main(void) {
122
123 scanf("%d%d%d", &A, &B, &C);
124 P ans = bfs();
125
126 if(ans.n) {
127 printf("%d\n", ans.n);
128 for(int i=0; i<ans.n; i++) {
129 int j = ans.p[i];
130 printf("%s\n", path[j]);
131 }
132 } else {
133 printf("impossible\n");
134 }
135
136 return 0;
137}