Openjudge Pots

Pots

总时间限制:
1000ms
内存限制:
65536kB
描述
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.

输入
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).
输出
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’.
样例输入
3 5 4
样例输出
6
FILL(2)
POUR(2,1)
DROP(1)
POUR(2,1)
FILL(2)
POUR(2,1)

本来我是不会做的,看了这篇题解 http://blog.csdn.net/kindlucy/article/details/5827794 之后明白的……

还是BFS

设(i,j)为瓶1与瓶2在某一时刻的容量,那么从这点出发,可以到达的点有:

(A, j) : FILL(1)

(i, B) : FILL(2)

(0, j): DROP(1)

(i, 0): DROP(2)

(i+j, 0) or (A, j-A+i) : POUR(2,1)

(0, i+j) or (i-B+j, B) : POUR(1,2)

判断这些点的访问状态,然后广搜下去,另外,要求出从源顶点到这些点的最短路径

当i=C 或 j=C时,就找到了

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<stack>
using namespace std;
/*
Starting From (0,0),End With (C,X) Or (X,C)
Operations:
FILL(1):(A,XB)
FILL(2):(XA,B)
DROP(1):(0,XB)
DROP(2):(XA,0)
POUR(2,1):
POUR(1,2):
*/

struct State{
    char C;//F:Fill;P:Pour;D:Drop
    int N1;
    int N2;
    int Parent_X;
    int Parent_Y;
};
int A,B,C;
int pos_x,pos_y;
State S[101][101];
bool visited[101][101];
inline void Fill_State(int x,int y,char type,int N1,int N2,int Parent_X,int Parent_Y){
    if(visited[x][y])return;
    S[x][y].C=type;
    S[x][y].N1=N1;
    S[x][y].N2=N2;
    S[x][y].Parent_X=Parent_X;
    S[x][y].Parent_Y=Parent_Y;
}
bool BFS(){
    queue<int> N1,N2;
    N1.push(0);
    N2.push(0);
    while(!N1.empty()){
        int t1=N1.front(),t2=N2.front();
        N1.pop();N2.pop();
        if(visited[t1][t2])continue;
        visited[t1][t2]=true;
        if(t1==C||t2==C){
            pos_x=t1;
            pos_y=t2;
            return true;
        }
        //Operations
        if(t1!=A){//FIll A
            N1.push(A);
            N2.push(t2);
            Fill_State(A,t2,'F',1,0,t1,t2);
        }
        if(t2!=B){//FILL B
            N1.push(t1);
            N2.push(B);
            Fill_State(t1,B,'F',2,0,t1,t2);
        }
        if(t1!=0){//DROP A
            N1.push(0);
            N2.push(t2);
            Fill_State(0,t2,'D',1,0,t1,t2);
        }
        if(t2!=0){//DROP B
            N1.push(t1);
            N2.push(0);
            Fill_State(t1,0,'D',2,0,t1,t2);
        }
        if(t1!=0&&t2!=B){//POUR(A,B)
            if(t1<=B-t2){
                N1.push(0);
                N2.push(t2+t1);
                Fill_State(0,t2+t1,'P',1,2,t1,t2);
            }else{
                N1.push(t1-B+t2);
                N2.push(B);
                Fill_State(t1-B+t2,B,'P',1,2,t1,t2);
            }
        }
        if(t1!=A&&t2!=0){//POUR(B,A)
            if(t2<=A-t1){
                N1.push(t2+t1);
                N2.push(0);
                Fill_State(t2+t1,0,'P',2,1,t1,t2);
            }else{
                N1.push(A);
                N2.push(t2-A+t1);
                Fill_State(A,t2-A+t1,'P',2,1,t1,t2);
            }
        }
    }
    return false;
}
int main(){
    scanf("%d%d%d",&A,&B,&C);
    memset(visited,false,sizeof(visited));
    S[0][0].Parent_X=-1;
    S[0][0].Parent_Y=-1;
    bool flag=BFS();
    if(!flag){
        printf("impossible");
    }else{
        stack<State *> Stack_ins;
        for(State * i=&S[pos_x][pos_y];!(i->Parent_X==-1&&i->Parent_Y==-1);i=&S[i->Parent_X][i->Parent_Y])Stack_ins.push(i);
        printf("%d\n",Stack_ins.size());
        while(!Stack_ins.empty()){
            switch(Stack_ins.top()->C){
            case 'F':
                printf("FILL(%d)\n",Stack_ins.top()->N1);
                break;
            case 'P':
                printf("POUR(%d,%d)\n",Stack_ins.top()->N1,Stack_ins.top()->N2);
                break;
            case 'D':
                printf("DROP(%d)\n",Stack_ins.top()->N1);
                break;
            }
            Stack_ins.pop();
        }
    }
}

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注