티스토리 뷰

728x90

https://www.acmicpc.net/problem/2251

 

2251번: 물통

각각 부피가 A, B, C(1≤A, B, C≤200) 리터인 세 개의 물통이 있다. 처음에는 앞의 두 물통은 비어 있고, 세 번째 물통은 가득(C 리터) 차 있다. 이제 어떤 물통에 들어있는 물을 다른 물통으로 쏟아 부

www.acmicpc.net

 

 

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
from collections import deque
 
 
def chk(x, y):
    if not visit[x][y]:
        visit[x][y] = True
        dq.append((x, y))
 
 
def bfs():
    while dq:
        x, y = dq.popleft()
        z = c - x - y
 
        if x == 0:
            answer.append(z)
 
        # x -> y
        water = min(x, b - y)
        chk(x - water, y + water)
        # x -> z
        water = min(x, c - z)
        chk(x - water, y)
        # y -> x
        water = min(y, a - x)
        chk(x + water, y - water)
        # y -> z
        water = min(y, c - z)
        chk(x, y - water)
        # z -> x
        water = min(z, a - x)
        chk(x + water, y)
        # z -> y
        water = min(z, b - y)
        chk(x, y + water)
 
 
a, b, c = map(int, input().split())
visit = [[False for _ in range(201)] for _ in range(201)]
visit[0][0] = True
dq = deque()
dq.append((0, 0))
answer = []
bfs()
answer.sort()
print(' '.join(map(str, answer)))
cs

 

 

댓글