Pots POJ

来源:互联网 发布:a星算法优化 编辑:程序博客网 时间:2024/06/15 18:19
Pots
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 18614 Accepted: 7880 Special Judge

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 potj is full (and there may be some water left in the pot i), or the poti 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 exactlyC liters of water in one of the pots.

Input

On the first and only line are the numbers A, B, andC. 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 operationsK. 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)

Source

Northeastern Europe 2002, Western Subregion

[Submit]   [Go Back]   [Status]   [Discuss]


这类题真的是长的恶心。。。

tho:  重点大概就在保存路径上。

对了这叫六个入口的BFS  还不算太多

#include <iostream>#include <queue>#include <cstring>#include <cstdio>#include <algorithm>#define inf 0x3f3f3f3f#define ms(x) memset(x,0,sizeof(x))using namespace std;const int N = 200;int A, B, C;struct node {    int a, b;    int op;    int pre;    int step;} q[N * N];bool vis[N][N];int id[N * N];int f,s,en;void bfs() {    node now, next;    q[0].a = 0;    q[0].b = 0;    q[0].pre = -1;    q[0].step = 0;    int head, tail;    head = tail = 0;    tail++;    vis[0][0] = 1;    while (head < tail) {             //用数组模拟队列的好处是保存编号        now = q[head];        head++;        for(int i = 1; i <= 6; i++) {            if(i==1){ // fill 1;                next.a = A;                next.b = now.b;            }            else if(i==2){ // fill 2;                next.b = B;                next.a = now.a;            }            else if(i==3){ // drop 1;                next.a = 0;                next.b = now.b;            }            else if(i==4){ // drop 2;                next.b = 0;                next.a = now.a;            }            else if(i==5){ // 1 to 2;                if(now.a + now.b > B) {                    next.b = B;                }                else {                    next.b = now.a + now.b;                }                next.a = now.a + now.b - next.b;            }            else if(i==6){ // 2 to 1                if(now.a + now.b > A) {                    next.a = A;                }                else {                    next.a = now.a + now.b;                }                next.b = now.a + now.b - next.a;            }            if(!vis[next.a][next.b]) {                    next.op = i;                    next.pre = head - 1;         //存前一个操作的编号                    vis[next.a][next.b] = 1;                    next.step = now.step + 1;                    q[tail++] = next;                    if(next.a == C || next.b == C) {                        f = 1;                        en = tail - 1;                        s = next.step;                        printf("%d\n", s);                        return ;                    }                }        }    }}int main() {    scanf("%d%d%d", &A, &B, &C);    ms(vis);    f=0;    bfs();    if(f) {        id[s] = en;                    //这是最后一步操作的编号        for(int i = s-1; i>=1;i--){            id[i] =  q[id[i+1]].pre; //向前遍历,找当前步的前一步        }        for(int i=1;i<=s;i++){            switch(q[id[i]].op){                case 1:                    puts("FILL(1)");                    break;                case 2:                    puts("FILL(2)");                    break;                case 3:                    puts("DROP(1)");                    break;                case 4:                    puts("DROP(2)");                    break;                case 5:                    puts("POUR(1,2)");                    break;                case 6:                    puts("POUR(2,1)");                    break;            }        }    }    else puts("impossible\n");    return 0;}