Pots (bfs)

来源:互联网 发布:淘宝规则名词解释 编辑:程序博客网 时间:2024/05/22 12:11

Question

You are given two pots, having the volume of A and B liters respectively. The following operations can be performed:
1.FILL(i) fill the pot i (1 ≤ i ≤ 2) from the tap;
2.DROP(i) empty the pot i to the drain;
3.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’.

Simple Input

3 5 4

Simple Output

6
FILL(2)
POUR(2,1)
DROP(1)
POUR(2,1)
FILL(2)
POUR(2,1)

Code

#include <iostream>#include <queue>#include <stack>using namespace std;int a, b, c;int ab[101][101];struct node{    int nx, ny;    int lx, ly;    int step;    int p1, p2, p3;};node AB[101][101];queue<node>q;node bfs(){    node start, mid, next;    start.lx = start.ly = 0;    start.nx = 0;    start.ny = 0;    start.step = 0;    start.p1 = start.p2 = start.p3 = 0;    AB[start.nx][start.ny] = start;    q.push(start);    while (!q.empty())    {        mid = q.front();        q.pop();        if (mid.nx == c || mid.ny == c)            return mid;        if (ab[mid.nx][mid.ny] == 1)            continue;        else            ab[mid.nx][mid.ny] = 1;        if (ab[a][mid.ny] != 1)        {            next = mid;            next.p1 = next.p2 = 1;            next.p3 = 0;            next.nx = a;            next.lx = mid.nx;            next.ly = mid.ny;            next.step++;            AB[next.nx][next.ny] = next;            q.push(next);        }        if (ab[mid.nx][b] != 1)        {            next = mid;            next.p1 = 1;            next.p2 = 2;            next.p3 = 0;            next.ny = b;            next.lx = mid.nx;            next.ly = mid.ny;            next.step++;            AB[next.nx][next.ny] = next;            q.push(next);        }        if (ab[0][mid.ny] != 1)        {            next = mid;            next.p1 = 2;            next.p2 = 1;            next.p3 = 0;            next.nx = 0;            next.lx = mid.nx;            next.ly = mid.ny;            next.step++;            AB[next.nx][next.ny] = next;            q.push(next);        }        if (ab[mid.nx][0] != 1)        {            next = mid;            next.p1 = 2;            next.p2 = 2;            next.p3 = 0;            next.ny = 0;            next.lx = mid.nx;            next.ly = mid.ny;            next.step++;            AB[next.nx][next.ny] = next;            q.push(next);        }        if (mid.ny < b)        {            next = mid;            if (next.nx + next.ny >= b)            {                next.nx = next.nx + next.ny - b;                next.ny = b;            }            else            {                next.ny = next.nx + next.ny;                next.nx = 0;            }            if (ab[next.nx][next.ny] != 1)            {                next.p1 = 3;                next.p2 = 1;                next.p3 = 2;                next.lx = mid.nx;                next.ly = mid.ny;                next.step++;                AB[next.nx][next.ny] = next;                q.push(next);            }        }        if (mid.nx < a)        {            next = mid;            if (next.nx + next.ny >= a)            {                next.ny = next.nx + next.ny - a;                next.nx = a;            }            else            {                next.nx = next.nx + next.ny;                next.ny = 0;            }            if (ab[next.nx][next.ny] != 1)            {                next.p1 = 3;                next.p2 = 2;                next.p3 = 1;                next.lx = mid.nx;                next.ly = mid.ny;                next.step++;                AB[next.nx][next.ny] = next;                q.push(next);            }        }    }    return start;}void charge(node mid){    if (mid.p1 == 1)        cout << "FILL(" << mid.p2 << ")" << endl;    else if (mid.p1 == 2)        cout << "DROP(" << mid.p2 << ")" << endl;    else if (mid.p1 == 3)        cout << "POUR(" << mid.p2 << "," << mid.p3 << ")" << endl;}void out(node mid){    stack<node>p;    cout << mid.step << endl;    while (mid.p1 != 0)    {        p.push(mid);        mid = AB[mid.lx][mid.ly];    }    while (!p.empty())    {        mid = p.top();        charge(mid);        p.pop();    }}int main(){    cin >> a >> b >> c;    memset(ab, 0, sizeof(ab));    node mid;    mid = bfs();    if (mid.p1 == 0)        cout << "impossible" << endl;    else        out(mid);    return 0;}

说明:感觉难点在于如何记录路径,因为bfs问题会遍历所有的点(找到最优解之前),所以当前访问的点会引出一个至多个点,故无法通过当前的点记录其下一个点,而每个点有只有一个母点,所以只需记录上一个点就可以了,最后通过栈实现正序输出。

原创粉丝点击