【题解】POJ-3414 Pots

Pots (POJ - 3414)

题面

You are given two pots, having the volume of A and B liters respectively. The following operations can be performed:

  1. FILL(i) fill the pot i (1 ≤ i ≤ 2) from the tap;
  2. DROP(i) empty the pot i to the drain;
  3. 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’.

样例输入

1
3 5 4

样例输出

1
2
3
4
5
6
7
6
FILL(2)
POUR(2,1)
DROP(1)
POUR(2,1)
FILL(2)
POUR(2,1)

提示

思路

倒水问题,bfs+路径输出

代码

查看代码
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int N = 1e2+5;

bool vis[N][N] = {{0}};
int A, B, C;

const char* path[] = {
"FILL(1)",
"FILL(2)",
"DROP(1)",
"DROP(2)",
"POUR(1,2)",
"POUR(2,1)"
};

struct P {
int a, b;
int p[N];
int n;
};

P bfs() {
memset(vis, 0, sizeof(vis));

P sp;
sp.a = 0;
sp.b = 0;
sp.n = 0;
memset(sp.p, 0, sizeof(sp.p));

queue<P> q;
q.push(sp);
vis[sp.a][sp.b] = 1;

while(!q.empty()) {
P tp = q.front();
q.pop();

if(tp.a==C || tp.b==C) {
return tp;
}

P np = tp;
np.n = tp.n+1;

if(tp.a<A) {
np.a = A;
np.b = tp.b;
if(!vis[np.a][np.b]) {
np.p[tp.n] = 0;
q.push(np);
vis[np.a][np.b] = 1;
}
}

if(tp.b<B) {
np.a = tp.a;
np.b = B;
if(!vis[np.a][np.b]) {
np.p[tp.n] = 1;
q.push(np);
vis[np.a][np.b] = 1;
}
}

if(tp.a) {
np.a = 0;
np.b = tp.b;
if(!vis[np.a][np.b]) {
np.p[tp.n] = 2;
q.push(np);
vis[np.a][np.b] = 1;
}
}

if(tp.b) {
np.a = tp.a;
np.b = 0;
if(!vis[np.a][np.b]) {
np.p[tp.n] = 3;
q.push(np);
vis[np.a][np.b] = 1;
}
}

if(tp.a && tp.b<B) {
if(tp.a > B-tp.b) {
np.a = tp.a-B+tp.b;
np.b = B;
} else {
np.a = 0;
np.b = tp.a+tp.b;
}
if(!vis[np.a][np.b]) {
np.p[tp.n] = 4;
q.push(np);
vis[np.a][np.b] = 1;
}
}

if(tp.b && tp.a<A) {
if(tp.b > A-tp.a) {
np.a = A;
np.b = tp.b-A+tp.a;
} else {
np.a = tp.a+tp.b;
np.b = 0;
}
if(!vis[np.a][np.b]) {
np.p[tp.n] = 5;
q.push(np);
vis[np.a][np.b] = 1;
}
}
}
return sp;
}

int main(void) {

scanf("%d%d%d", &A, &B, &C);
P ans = bfs();

if(ans.n) {
printf("%d\n", ans.n);
for(int i=0; i<ans.n; i++) {
int j = ans.p[i];
printf("%s\n", path[j]);
}
} else {
printf("impossible\n");
}

return 0;
}
_/_/_/_/_/ EOF _/_/_/_/_/