알고리즘
[백준 알고리즘] 2251번 물통. 파이썬(python)/ dfs, bfs
삶은겨란
2022. 10. 19. 16:04
https://www.acmicpc.net/problem/2251
2251번: 물통
각각 부피가 A, B, C(1≤A, B, C≤200) 리터인 세 개의 물통이 있다. 처음에는 앞의 두 물통은 비어 있고, 세 번째 물통은 가득(C 리터) 차 있다. 이제 어떤 물통에 들어있는 물을 다른 물통으로 쏟아 부
www.acmicpc.net
import sys
from collections import deque
input=sys.stdin.readline
a,b,c=map(int, input().split())
# 경우의 수 담을 큐
q=deque()
q.append((0,0))
visit=[[0]*(b+1) for _ in range(a+1)]
visit[0][0]=1
answer=[]
def pour(x,y):
if visit[x][y]==0:
visit[x][y]=1
q.append((x,y))
def bfs():
while q:
x,y=q.popleft()
z=c-x-y
if x==0: # A물통이 비어있는 경우 C무롱에 남은 양 저장
answer.append(z)
# A->B
water=min(x,b-y)
pour(x-water, y+water)
# A->C
water=min(x,c-z)
pour(x-water, y)
# B->A
water=min(y,a-x)
pour(x+water, y-water)
# B->C
water=min(y,c-z)
pour(x, y-water)
# C->A
water=min(z,a-x)
pour(x+water, y)
# C->B
water=min(z,b-y)
pour(x, y+water)
bfs()
answer.sort()
print(*answer)