POJ 3414 Pots (BFS+路径打印)

来源:互联网 发布:java微信卡券开发实例 编辑:程序博客网 时间:2024/05/16 07:11

Description
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.

Input
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).

Output
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’.

Sample Input
3 5 4
Sample Output
6
FILL(2)
POUR(2,1)
DROP(1)
POUR(2,1)
FILL(2)
POUR(2,1)

题目大意:经过一定操作后,使得a和b瓶任意一瓶的水达到c。

难点是要打印得到最优解的过程,对于BFS题来说,这点比较苛刻。

另外可以用递归方式最优解,但可能会引起栈溢出,故用vector保存最好,废话不多说了,直接上代码。

#include <iostream>#include <cstring>#include <queue>#include <vector>using namespace std;int a, b, c;bool look[110][110];//#define Night_13class node {public:    int cura, curb, op, step;    node(int a1 = 0, int a2 = 0, int a3 = 0, int a4 = 0) {        cura = a1, curb = a2, op = a3, step = a4;    }} father[110][110];void operate(int id) {    switch (id) {    case 11: printf("FILL(1)\n"); return;    case 12: printf("FILL(2)\n"); return;    case 21: printf("DROP(1)\n"); return;    case 22: printf("DROP(2)\n"); return;    case 31: printf("POUR(1,2)\n"); return;    case 32: printf("POUR(2,1)\n");    }}void printPath(node nod) {    vector<node> v;    int len = 0;    while (nod.step != 0) {        ++len;        v.push_back(nod);        nod = father[nod.cura][nod.curb];    }    for (int i = len - 1; i >= 0; --i) {        operate(v[i].op);    }}void bfs() {    memset(look, 0, sizeof(look));    queue<node> q;    q.push(node(0, 0, 0, 0));    look[a][b] = true;    father[0][0] = node(0, 0, 0, 0);    while (!q.empty()) {        node head = q.front(); q.pop();        if (head.cura == c || head.curb == c) {            printf("%d\n", head.step);            printPath(head);            return;        }        for (int i = 0; i < 6; ++i) {            int nexta = head.cura, nextb = head.curb, op;            switch (i) {            case 0:                if (nexta == a) continue;                op = 11, nexta = a; break;            case 1:                 if (nextb == b) continue;                op = 12, nextb = b; break;            case 2:                 if (nexta == 0) continue;                op = 21, nexta = 0; break;            case 3:                 if (nextb == 0) continue;                op = 22, nextb = 0; break;            case 4:                if (nexta == 0) continue;                op = 31;                if (nexta <= b - nextb) {                    nextb += nexta;                    nexta = 0;                } else {                    nexta -= b - nextb;                    nextb = b;                }                break;            case 5:                if (nextb == 0) continue;                op = 32;                if (nextb <= a - nexta) {                    nexta += nextb;                    nextb = 0;                } else {                    nextb -= a - nexta;                    nexta = a;                }                break;            }            if (look[nexta][nextb]) continue;            look[nexta][nextb] = true;            father[nexta][nextb] = node(head.cura, head.curb, head.op, head.step);            q.push(node(nexta, nextb, op, head.step + 1));        }    }    printf("impossible\n");}int main() {#ifdef Night_13    freopen("input.txt", "r", stdin);#endif    while (scanf("%d%d%d", &a, &b, &c) != EOF) {        bfs();    }    return 0;}
2 0
原创粉丝点击