bfs不难想,也很好写。
但写的很搓,因为六种方法一个个写了,就导致代码很长。对于几个方向那样的题可以用数组把方向保存下来,但这个就不行了。
bfs是很好写,找到总共要几步也就很简单。但要还原出路径不是非常顺手,因为经常写的题目是不需要输出路径,只要结果,即需要几步操作即可。但总算是硬着头皮写完了,用了近50分钟。
路漫漫其修远兮……
保存路径是用的两个vector。第一个vector把所有经过的点都存下来,第二个vector在bfs完之后从第一个vector中找路径然后输出。找路径时从最后开始向前找,一直找到开始的点即可。往前找用pre保存下了下标。
代码很长很搓,但应该还是比较清楚的,毕竟代码中很多都是重复的工作而已。
/*
POJ: 3414 Pots
*/
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#define M 105
using namespace std;
struct Node {
int x;
int y;
int step;
Node(int _x, int _y, int _step) : x(_x), y(_y), step(_step) {}
};
struct Path {
int method;
int pre;
Path(int _m, int _p) : method(_m), pre(_p) {}
};
int a, b, c;
bool visited[M][M];
queue<struct Node> que;
vector<struct Path> vec;
vector<int> path;
int bfs()
{
memset(visited, false, sizeof(visited));
vec.clear();
while(!que.empty())
que.pop();
que.push(Node(0, 0, 0));
visited[0][0] = true;
vec.push_back(Path(-1, -1));
int index = -1;
while(!que.empty()) {
index++;
struct Node tmp = que.front();
que.pop();
int tmpx, tmpy;
tmpx = a;
tmpy = tmp.y;
if(!visited[tmpx][tmpy]) {
vec.push_back(Path(1, index));
visited[tmpx][tmpy] = true;
if(tmpx == c || tmpy == c)
return tmp.step + 1;
else
que.push(Node(tmpx, tmpy, tmp.step + 1));
}
tmpx = tmp.x;
tmpy = b;
if(!visited[tmpx][tmpy]) {
vec.push_back(Path(2, index));
visited[tmpx][tmpy] = true;
if(tmpx == c || tmpy == c)
return tmp.step + 1;
else
que.push(Node(tmpx, tmpy, tmp.step + 1));
}
if(tmp.x + tmp.y <= b && tmp.x > 0) {
tmpx = 0;
tmpy = tmp.x + tmp.y;
}
else if(tmp.x > 0){
tmpx = tmp.x + tmp.y - b;
tmpy = b;
}
if(!visited[tmpx][tmpy]) {
vec.push_back(Path(3, index));
visited[tmpx][tmpy] = true;
if(tmpx == c || tmpy == c)
return tmp.step + 1;
else
que.push(Node(tmpx, tmpy, tmp.step + 1));
}
if(tmp.x + tmp.y <= a && tmp.y > 0) {
tmpx = tmp.x + tmp.y;
tmpy = 0;
}
else if(tmp.y > 0){
tmpx = a;
tmpy = tmp.x + tmp.y - a;
}
if(!visited[tmpx][tmpy]) {
vec.push_back(Path(4, index));
visited[tmpx][tmpy] = true;
if(tmpx == c || tmpy == c)
return tmp.step + 1;
else
que.push(Node(tmpx, tmpy, tmp.step + 1));
}
tmpx = 0;
tmpy = tmp.y;
if(!visited[tmpx][tmpy]) {
vec.push_back(Path(5, index));
visited[tmpx][tmpy] = true;
if(tmpx == c || tmpy == c)
return tmp.step + 1;
else
que.push(Node(tmpx, tmpy, tmp.step + 1));
}
tmpx = tmp.x;
tmpy = 0;
if(!visited[tmpx][tmpy]) {
vec.push_back(Path(6, index));
visited[tmpx][tmpy] = true;
if(tmpx == c || tmpy == c)
return tmp.step + 1;
else
que.push(Node(tmpx, tmpy, tmp.step + 1));
}
}
return -1;
}
int main()
{
freopen("data.in", "rb", stdin);
while(scanf("%d%d%d", &a, &b, &c) != EOF) {
int res = bfs();
if(res == -1)
printf("impossible\n");
else {
printf("%d\n", res);
path.clear();
int i = vec.size() - 1;
while(vec[i].pre != -1) {
path.push_back(vec[i].method);
i = vec[i].pre;
}
for(vector<int>::reverse_iterator it = path.rbegin(); it != path.rend(); it++) {
switch(*it) {
case 1: printf("FILL(1)\n");
break;
case 2: printf("FILL(2)\n");
break;
case 3: printf("POUR(1,2)\n");
break;
case 4: printf("POUR(2,1)\n");
break;
case 5: printf("DROP(1)\n");
break;
case 6: printf("DROP(2)\n");
break;
}
}
}
}
return 0;
}