#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
int a, b, c;
int vist[105][105];
struct node {
int x, y;
int id;
int op;
int num;
int pre;
}s[100005];
int cnt;
node cur , next;
node judge(node cur, int k) { //6种状态的处理代码;
if(k == 1)
cur.x = 0;
else if(k == 2)
cur.y = 0;
else if(k == 3)
cur.x = a;
else if(k == 4) {
cur.y = b;
}else if(k == 5) {
if(b - cur.y < cur.x) {
cur.x = cur.x - (b - cur.y);
cur.y = b;
}else {
cur.y += cur.x;
cur.x = 0;
}
}else {
if(a - cur.x < cur.y) {
cur.y = cur.y - (a - cur.x);
cur.x = a;
}else {
cur.x += cur.y;
cur.y = 0;
}
}
return cur;
}
int dfs(int ta, int tb) {
queue<node>que;
s[0].id = 0;
s[0].x = ta;
s[0].y = tb;
s[0].num = 0;
s[0].op = 0;
s[0].pre = 0;
cnt++;
vist[ta][tb] = 1;
que.push(s[0]);
while(!que.empty()) {
next = que.front();
que.pop();
// printf("%d %d %d %d\n", next.x, next.y, next.pre, next.id);
if(next.x == c || next.y == c) {
return next.id;
}
for(int i = 1; i <= 6; i++) {
// printf("%d %d, %d\n", cur.x, cur.y, i);
cur = judge(next, i);
// printf("h %d %d, %d\n", cur.x, cur.y, i);
if(!vist[cur.x][cur.y]) {
// printf("hh %d %d\n", cur.x, cur.y);
s[cnt].pre = next.id;
s[cnt].id = cnt;
s[cnt].x = cur.x;
s[cnt].y = cur.y;
s[cnt].num = cur.num + 1;
s[cnt++].op = i;
que.push(s[cnt-1]);
vist[cur.x][cur.y] = 1;
}
}
}
return -1;
}
void put(int i) { //处理的6个状态对应的操作输出。
if(i == 1) {
printf("DROP(1)\n");
}else if(i == 2) {
printf("DROP(2)\n");
}else if(i == 3) {
printf("FILL(1)\n");
}else if(i == 4) {
printf("FILL(2)\n");
}else if(i == 5) {
printf("POUR(1,2)\n");
}else {
printf("POUR(2,1)\n");
}
}
void output(int k) { //递归输出结果, 即反推回去;
if(s[k].pre == 0) {
put(s[k].op);
}else{
output(s[k].pre);
put(s[k].op);
}
}
int main()
{
while(scanf("%d%d%d", &a, &b, &c) != EOF) {
memset(vist, 0, sizeof(vist));
cnt = 0;
int ans = dfs(0, 0);
if(ans == -1)
printf("impossible\n");
else {
printf("%d\n", s[ans].num);
output(ans);
}
}
return 0;
}
poj3414 pots(经典搜索题目, 题目本身不难,处理有点繁琐)
最新推荐文章于 2018-12-26 14:18:05 发布