반응형
Notice
Recent Posts
Recent Comments
- Today
- Total
작심삼일
[백준] 2251번 | 물통 | Python 본문
728x90
반응형
문제 링크: https://www.acmicpc.net/problem/2251
문제
각각 부피가 A, B, C(1≤A, B, C≤200) 리터인 세 개의 물통이 있다. 처음에는 앞의 두 물통은 비어 있고, 세 번째 물통은 가득(C 리터) 차 있다. 이제 어떤 물통에 들어있는 물을 다른 물통으로 쏟아 부을 수 있는데, 이때에는 한 물통이 비거나, 다른 한 물통이 가득 찰 때까지 물을 부을 수 있다. 이 과정에서 손실되는 물은 없다고 가정한다.
이와 같은 과정을 거치다보면 세 번째 물통(용량이 C인)에 담겨있는 물의 양이 변할 수도 있다. 첫 번째 물통(용량이 A인)이 비어 있을 때, 세 번째 물통(용량이 C인)에 담겨있을 수 있는 물의 양을 모두 구해내는 프로그램을 작성하시오.
입력
첫째 줄에 세 정수 A, B, C가 주어진다.
출력
첫째 줄에 공백으로 구분하여 답을 출력한다. 각 용량은 오름차순으로 정렬한다.
내 풀이
물통의 물을 이리저리 직접 옮겨보면 된다.
다만 여기서 주의할 점은, A의 물을 B에 부을 때 두가지 경우가 생긴다는 점이다.
* A의 용량이 10, B의 용량이 20일 때 *
1. A에 물이 10 있고, B에 물이 15 있는 경우 $\rightarrow$ A: 5, B: 20
2. A에 물이 10 있고, B에 물이 5 있는 경우 $\rightarrow$ A: 0, B:15
코드
temp = input()
[a, b, c] = temp.split()
A = int(a)
B = int(b)
C = int(c)
q = [[0, 0, C]]
n = 0
answer = []
while n < len(q):
[a, b, c] = q[n]
# print(q[n])
if a == 0:
answer.append(c)
if a + b > B: # A -> B
new_A = a - B + b
new_B = B
else:
new_A = 0
new_B = a + b
new_C = c
if [new_A, new_B, new_C] not in q:
# print(1, [new_A, new_B, new_C])
q.append([new_A, new_B, new_C])
if a + c > C: # A -> C
new_A = a - C + c
new_C = C
else:
new_A = 0
new_C = a + c
new_B = b
if [new_A, new_B, new_C] not in q:
# print(2, [new_A, new_B, new_C])
q.append([new_A, new_B, new_C])
if a + b > A: # B -> A
new_A = A
new_B = a - A + b
else:
new_A = a + b
new_B = 0
new_C = c
if [new_A, new_B, new_C] not in q:
# print(3, [new_A, new_B, new_C])
q.append([new_A, new_B, new_C])
if b + c > C: # B -> C
new_B = b + c - C
new_C = C
else:
new_B = 0
new_C = b + c
new_A = a
if [new_A, new_B, new_C] not in q:
# print(4, [new_A, new_B, new_C])
q.append([new_A, new_B, new_C])
if a + c > A: # C -> A
new_A = A
new_C = a + c - A
else:
new_A = a + c
new_C = 0
new_B = b
if [new_A, new_B, new_C] not in q:
# print(5, [new_A, new_B, new_C])
q.append([new_A, new_B, new_C])
if b + c > B: # C -> B
new_B = B
new_C = b + c - B
else:
new_B = b + c
new_C = 0
new_A = a
if [new_A, new_B, new_C] not in q:
# print(6, [new_A, new_B, new_C])
q.append([new_A, new_B, new_C])
n += 1
answer = list(set(answer))
answer.sort()
for x in range(len(answer)):
print(answer[x], end=' ')
728x90
반응형
'스터디 > 코테' 카테고리의 다른 글
[LeetCode] 1200 | Minimum Absolute Difference | Python (0) | 2021.12.20 |
---|---|
[백준] 2579번 | 계단 오르기 | C++ (0) | 2021.12.20 |
[백준] 2225번 | 합분해 | C++ (0) | 2021.12.15 |
[백준] 2110번 | 공유기 설치 | C++ (0) | 2021.12.09 |
[백준] 2011번 | 암호코드 | C++ (0) | 2021.12.08 |
Comments