POJ - 3414 Pots(BFS)

来源:互联网 发布:mac开机一半自动关机 编辑:程序博客网 时间:2024/04/28 07:15
I - Pots
Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u
Submit Status

Description

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

Sample Input

3 5 4

Sample Output

6FILL(2)POUR(2,1)DROP(1)POUR(2,1)FILL(2)POUR(2,1)
按照题目要求,直接BFS就可以了
#include <iostream>#include <string>#include <cstdio>#include <cstring>#include <algorithm>#include <queue>using namespace std;const int MAXN = 100 + 5;int A, B, C;bool vis[MAXN][MAXN];struct o {    int a, b;    string str;    o() {}    o(int a, int b, string str):a(a), b(b), str(str) {}};queue<o> Q;string X[6] = {"FILL(1)", "FILL(2)", "DROP(1)", "DROP(2)", "POUR(1,2)", "POUR(2,1)"};string z[6] = {"0", "1", "2", "3", "4", "5"};bool BFS() {    memset(vis, false, sizeof(vis));    Q.push(o(0, 0, ""));    while(!Q.empty()) {        o e = Q.front();        Q.pop();        if(vis[e.a][e.b]) continue;        vis[e.a][e.b] = true;        if(e.a == C || e.b == C) {            int len = e.str.length();            printf("%d\n", len);            for(int i = 0; i < len; i ++) {                cout << X[e.str[i] - '0'] << endl;            }            return true;        }        for(int i = 0; i < 6; i ++) {//6种情况处理即可            o h = e;            switch(i) {            case 0 :                h.a = A;                break;            case 1:                h.b = B;                break;            case 2:                h.a = 0;                break;            case 3:                h.b = 0;                break;            case 4:                if(e.b + e.a < B) {                    h.b = e.b + e.a;                    h.a = 0;                } else {                    h.b = B;                    h.a = e.b + e.a - B;                }                break;            case 5:                if(e.b + e.a < A) {                    h.a = e.a + e.b;                    h.b = 0;                } else {                    h.a = A;                    h.b = e.b + e.a - A;                }                break;            }            h.str = e.str + z[i];            Q.push(h);        }    }    return false;}int main() {    while(~scanf("%d%d%d", &A, &B, &C)) {        bool i = BFS();        if(!i) {            printf("impossible\n");        }    }    return 0;}



1 0
原创粉丝点击