ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

728x90
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ

 

 

2251๋ฒˆ: ๋ฌผํ†ต

๊ฐ๊ฐ ๋ถ€ํ”ผ๊ฐ€ A, B, C(1≤A, B, C≤200) ๋ฆฌํ„ฐ์ธ ์„ธ ๊ฐœ์˜ ๋ฌผํ†ต์ด ์žˆ๋‹ค. ์ฒ˜์Œ์—๋Š” ์•ž์˜ ๋‘ ๋ฌผํ†ต์€ ๋น„์–ด ์žˆ๊ณ , ์„ธ ๋ฒˆ์งธ ๋ฌผํ†ต์€ ๊ฐ€๋“(C ๋ฆฌํ„ฐ) ์ฐจ ์žˆ๋‹ค. ์ด์ œ ์–ด๋–ค ๋ฌผํ†ต์— ๋“ค์–ด์žˆ๋Š” ๋ฌผ์„ ๋‹ค๋ฅธ ๋ฌผํ†ต์œผ๋กœ ์Ÿ์•„ ๋ถ€

www.acmicpc.net

 

๋ฌธ์ œ ํ’€์ด

 A, B, C์˜ ๋ฌผํ†ต์ด ์žˆ๊ณ , ์ฒ˜์Œ์—๋Š” C์˜ ๋ฌผํ†ต์—๋งŒ ๋ฌผ์ด ๊ฐ€๋“์ฐจ ์žˆ๋‹ค. ๋ฌผํ†ต์— ๋ฌผ์„ ํ•œ๋ฒˆ์— ๋‹ค๋ฅธ ํ†ต์œผ๋กœ ์ด๋™์‹œํ‚ค๋Š” ๊ฒฝ์šฐ, A๊ฐ€ ๋น„์–ด์žˆ๋Š” ๊ฒฝ์šฐ์— C์˜ ๋ฌผํ†ต์— ์ฐจ์žˆ๋Š” ๋ฌผ์˜ ์–‘์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฌธ์ œ์ด๋‹ค. ๋ฌผ์„ ์˜ฎ๊ธธ ์ˆ˜ ์žˆ๋Š” ๊ฒฝ์šฐ์˜ ์ˆ˜๋Š” 6๊ฐ€์ง€๊ฐ€ ์กด์žฌํ•œ๋‹ค. ์ด๋Š” ์ˆœ์—ด๋กœ DFS๋‚˜ permutaion์„ ์ด์šฉํ•ด ๊ตฌํ•ด๋„ ๋˜์ง€๋งŒ, `A → B`, `A → C`, `B → A`, `B → C`, `C → A`, `C → B`์™€ ๊ฐ™์€ ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ํ•˜๋“œ์ฝ”๋”ฉํ•˜์—ฌ ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ๋ถ„๋ฅ˜ํ•˜์˜€๋‹ค.

 

 ๋ฌธ์ œ๋ฅผ ํ’€๊ธฐ ์œ„ํ•ด BFS๋ฅผ ํ†ตํ•ด ๋™์ผํ•œ ๊ฒฝ์šฐ์˜ ์ˆ˜์ธ์ง€ ํŒ๋‹จํ•˜๋ฉด์„œ, ๋ฌผ์„ ์˜ฎ๊ฒป์„ ๋•Œ A๊ฐ€ ๋น„๋Š”์ง€ ํ™•์ธํ•˜๋ฉด ๋ฌธ์ œ์—์„œ ์›ํ•˜๋Š” ๋‹ต์„ ๋ฐ˜ํ™˜ํ•  ์ˆ˜ ์žˆ๋‹ค.

 

์ฝ”๋“œ

from sys import stdin
from collections import deque, defaultdict


def bfs(a, b, c):
    q = deque([[a, b, c]])
    check[tuple(q[0])] = True

    while q:
        cur_case = q.popleft()

        if not cur_case[A]:
            answer.append(cur_case[C])

        for rm_water, add_water in cases:
            next_case = cur_case[:]

            if cur_case[rm_water] + cur_case[add_water] > bucket_size[add_water]:
                next_case[rm_water] = \
                    cur_case[rm_water] + cur_case[add_water] - bucket_size[add_water]
                next_case[add_water] = bucket_size[add_water]
            else:
                next_case[add_water] = cur_case[rm_water] + cur_case[add_water]
                next_case[rm_water] = 0

            if not check[tuple(next_case)]:
                check[tuple(next_case)] = True
                q.append(next_case)

if __name__ == '__main__':
    answer = []
    A, C = 0, 2
    cases = ((0, 1), (0, 2), (1, 0), (1, 2), (2, 0), (2, 1))
    check = defaultdict(bool)
    bucket_size = list(map(int, stdin.readline().split()))
    bfs(0, 0, bucket_size[C])
    print(*sorted(answer))
728x90
๋ฐ˜์‘ํ˜•
๋Œ“๊ธ€
๊ธ€ ๋ณด๊ด€ํ•จ
์ตœ๊ทผ์— ์˜ฌ๋ผ์˜จ ๊ธ€
์ตœ๊ทผ์— ๋‹ฌ๋ฆฐ ๋Œ“๊ธ€