summaryrefslogtreecommitdiff
path: root/1.5/milk3.c
blob: f50a61d39f179d7adf30507b9641bbb0f2d05541 (plain)
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
ID: mytbk921
LANG: C
TASK: milk3
*/

#include <stdio.h>
#include <string.h>

int possible[21];
int searched[21][21][21][3];

typedef struct
{
    int amount;
    int capacity;
}bucket;

void test_0(bucket, bucket, bucket);
void test_1(bucket, bucket, bucket);
void test_2(bucket, bucket, bucket);

void pour(const bucket *a, const bucket *b, bucket *_a, bucket *_b)
{
    _a->capacity = a->capacity;
    _b->capacity = b->capacity;
    
    if (b->amount+a->amount<=b->capacity){
        _a->amount = 0;
        _b->amount = a->amount+b->amount;
    }else{
        _a->amount = a->amount-(b->capacity-b->amount);
        _b->amount = b->capacity;
    }
}
    
void test_0(bucket a, bucket b, bucket c)
{
    if (a.amount==0){
        possible[c.amount] = 1;
        return;
    }
    
    if (searched[a.amount][b.amount][c.amount][0]){
        return;
    }else{
        searched[a.amount][b.amount][c.amount][0] = 1;
    }
    
    bucket _a,_b,_c;
    if (b.amount!=b.capacity){
        pour(&a, &b, &_a, &_b);
        if (_a.amount==0){
            possible[c.amount] = 1;
        }
        test_1(_a, _b, c);
        test_2(_a, _b, c);
    }
    if (c.amount!=c.capacity){
        pour(&a, &c, &_a, &_c);
        if (_a.amount==0){
            possible[_c.amount] = 1;
        }
        test_1(_a, b, _c);
        test_2(_a, b, _c);
    }
}

void test_1(bucket a, bucket b, bucket c)
{
    if (b.amount==0){
        return;
    }
    
    if (searched[a.amount][b.amount][c.amount][1]){
        return;
    }else{
        searched[a.amount][b.amount][c.amount][1] = 1;
    }
    
    bucket _a,_b,_c;
    if (a.amount!=a.capacity){
        pour(&b, &a, &_b, &_a);
        test_0(_a, _b, c);
        test_2(_a, _b, c);
    }
    if (c.amount!=c.capacity){
        pour(&b, &c, &_b, &_c);
        test_0(a, _b, _c);
        test_2(a, _b, _c);
    }
}

void test_2(bucket a, bucket b, bucket c)
{
    if (c.amount==0){
        return;
    }
    
    if (searched[a.amount][b.amount][c.amount][2]){
        return;
    }else{
        searched[a.amount][b.amount][c.amount][2] = 1;
    }
    
    bucket _a,_b,_c;
    if (a.amount!=a.capacity){
        pour(&c, &a, &_c, &_a);
        test_0(_a, b, _c);
        test_1(_a, b, _c);
    }
    if (b.amount!=b.capacity){
        pour(&c, &b, &_c, &_b);
        test_0(a, _b, _c);
        test_1(a, _b, _c);
    }
}

int main()
{
    FILE *fin, *fout;
    bucket a, b, c;
    int i;
    
    a.amount = b.amount = 0;
    
    fin = fopen("milk3.in", "r");
    fout = fopen("milk3.out", "w");
    fscanf(fin, "%d%d%d", &a.capacity, &b.capacity, &c.capacity);
    fclose(fin);
    
    c.amount = c.capacity;

    memset(possible, 0, sizeof(possible));
    memset(searched, 0, sizeof(searched));

    possible[c.amount] = 1;
    test_2(a, b, c);

    for (i=0; i<c.amount; i++){
        if (possible[i]){
            fprintf(fout, "%d ", i);
        }
    }
    fprintf(fout, "%d\n", c.amount);
    fclose(fout);
    return 0;
}