记录a,b水位分别作为x,y来存储bfs扩展出的全图
分为六种情况,然后按照每种情况来拓展,最后得到答案
#include<iostream>
#include<queue>
#include<stack>
using namespace std;
int a, b, c;
struct MyStruct
{
int a, b;//记录a,b水位
int step;//记录多少步
int status;//记录状态
int prex;//记录前一个x
int prey;//记录前一个y
};
queue<MyStruct>que;
MyStruct ans;
MyStruct id[10010][1010];//记录bfs扩展出的图
int vis[1010][1010];//记录是否访问过
int flag;//标记
void bfs() {//标准bfs
MyStruct t;
t.a = 0;
t.b = 0;
t.status = 0;
t.step = 1;
id[0][0] = t;
que.push(t);
while (!que.empty()) {
MyStruct now, next;
now = que.front();
que.pop();
if (now.a == c || now.b == c) {
ans = now;
flag = 1;
return;
}
for (int i = 0; i < 6; i++) {//分为6种情况
if (i == 0) {//a满
next.a = a;
next.b = now.b;
}
else if (i == 1) {//b满
next.a = now.a;
next.b = b;
}
else if (i == 2) {//a空
next.a = 0;
next.b = now.b;
}
else if (i == 3) {
next.a = now.a;
next.b = 0;
}
else if (i == 4) {//a装b
if (now.a + now.b <= b) {//小于等于
next.a = 0;
next.b = now.a + now.b;
}
else {//大于
next.a = now.a + now.b - b;
next.b = b;
}
}
else{//b装a
if (now.b + now.a <= a) {//小于等于
next.a = now.a + now.b;
next.b = 0;
}
else {//大于
next.b = now.a + now.b - a;
next.a = a;
}
}
next.status = i;
if (vis[next.a][next.b] == 0) {//入队
vis[next.a][next.b] = 1;
next.step = now.step + 1;//步数+1
next.prex = now.a;//记录前一个x
next.prey = now.b;//记录前一个y
id[next.a][next.b] = next;//记录扩展出的图
que.push(next);
}
}
}
}
int main()
{
while (cin >> a >> b >> c) {
stack<int>num;
flag = 0;
bfs();
if (flag) {
MyStruct t = ans;
for (int i = ans.step-1; i >0; i--) {
int x, y;
x = t.prex;
y = t.prey;
num.push(t.status);
t = id[x][y];
}
cout << num.size()<< endl;
while (!num.empty()) {
int T = num.top();
num.pop();
if (T == 0) {
cout << "FILL(1)" << endl;
}
else if (T == 1) {
cout << "FILL(2)" << endl;
}
else if (T == 2) {
cout << "DROP(1)" << endl;
}
else if (T == 3) {
cout << "DROP(2)" << endl;
}
else if (T == 4) {
cout << "POUR(1,2)" << endl;
}
else if (T == 5) {
cout << "POUR(2,1)" << endl;
}
}
}
else {
cout << "impossible" << endl;
}
}
return 0;
}