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’.
3 5 4Sample Output
6 FILL(2) POUR(2,1) DROP(1) POUR(2,1) FILL(2) POUR(2,1)
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <cstring>
using namespace std;
typedef long long ll;
typedef struct node {
int i, j;
string path;
} node;
const int maxn = 1e3 + 10;
int t, n, m, cnt, cnt2;
int vis[maxn][maxn];
char s[maxn], s2[maxn], s3[maxn], str[maxn];
bool BFS() {
memset(vis, 0, sizeof(vis));
queue<node> Q;
node p;
p.i = n;
p.j = 0;
p.path = '1';
Q.push(p);
p.i = 0;
p.j = m;
p.path = '2';
Q.push(p);
vis[0][m] = vis[n][0] = 1;
while(!Q.empty()) {
p = Q.front();
Q.pop();
if(p.i == t || p.j == t) {
int len = p.path.size();
cout << len << endl;
for(int i = 0; i < len; i++) {
if(p.path[i] == '1') {
cout << "FILL(1)\n";
} else if(p.path[i] == '2') {
cout << "FILL(2)\n";
} else if(p.path[i] == '3') {
cout << "DROP(1)\n";
} else if(p.path[i] == '4') {
cout << "DROP(2)\n";
} else if(p.path[i] == '5') {
cout << "POUR(1,2)\n";
} else {
cout << "POUR(2,1)\n";
}
}
return 1;
} else {
node nn;
nn.i = n;
nn.j = p.j;
nn.path = p.path + '1';
if(!vis[nn.i][nn.j]) {
Q.push(nn);
vis[nn.i][nn.j] = 1;
}
nn.i = p.i;
nn.j = m;
nn.path = p.path + '2';
if(!vis[nn.i][nn.j]) {
Q.push(nn);
vis[nn.i][nn.j] = 1;
}
nn.i = 0;
nn.j = p.j;
nn.path = p.path + '3';
if(!vis[nn.i][nn.j]) {
Q.push(nn);
vis[nn.i][nn.j] = 1;
}
nn.i = p.i;
nn.j = 0;
nn.path = p.path + '4';
if(!vis[nn.i][nn.j]) {
Q.push(nn);
vis[nn.i][nn.j] = 1;
}
if(p.i + p.j > m) {
nn.j = m;
nn.i = p.i + p.j - m;
} else {
nn.i = 0;
nn.j = p.i + p.j;
}
nn.path = p.path + '5';
if(!vis[nn.i][nn.j]) {
Q.push(nn);
vis[nn.i][nn.j] = 1;
}
if(p.i + p.j > n) {
nn.i = n;
nn.j = p.i + p.j - n;
} else {
nn.i = p.i + p.j;
nn.j = 0;
}
nn.path = p.path + '6';
if(!vis[nn.i][nn.j]) {
Q.push(nn);
vis[nn.i][nn.j] = 1;
}
}
}
return 0;
}
int main() {
ios::sync_with_stdio(false);
cin >> n >> m >> t;
int ans = BFS();
if(!ans) {
cout << "impossible\n";
}
return 0;
}
本文介绍了一种解决特定水罐问题的算法实现,通过BFS(宽度优先搜索)算法来寻找从两个不同容量的水罐中得到目标水量的最短操作步骤。该算法能够输出达到目标水量所需的精确操作序列,适用于容量范围为1到100升的水罐,并能处理无法达成目标的情况。
746

被折叠的 条评论
为什么被折叠?



