From 84695836098f8eba2e4170e5e444d386962ba130 Mon Sep 17 00:00:00 2001 From: SSUM <116950962+ssum21@users.noreply.github.com> Date: Thu, 27 Feb 2025 12:21:11 +0900 Subject: [PATCH] =?UTF-8?q?[Gold=20IV]=20Title:=20=EB=AC=BC=ED=86=B5,=20Ti?= =?UTF-8?q?me:=2036=20ms,=20Memory:=2032456=20KB=20-BaekjoonHub?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 백준/Gold/2251. 물통/README.md | 30 ++++++++++++ 백준/Gold/2251. 물통/물통.py | 89 ++++++++++++++++++++++++++++++++++ 2 files changed, 119 insertions(+) create mode 100644 백준/Gold/2251. 물통/README.md create mode 100644 백준/Gold/2251. 물통/물통.py diff --git a/백준/Gold/2251. 물통/README.md b/백준/Gold/2251. 물통/README.md new file mode 100644 index 0000000..b540bc6 --- /dev/null +++ b/백준/Gold/2251. 물통/README.md @@ -0,0 +1,30 @@ +# [Gold IV] 물통 - 2251 + +[문제 링크](https://www.acmicpc.net/problem/2251) + +### 성능 요약 + +메모리: 32456 KB, 시간: 36 ms + +### 분류 + +너비 우선 탐색, 깊이 우선 탐색, 그래프 이론, 그래프 탐색 + +### 제출 일자 + +2025년 2월 27일 12:21:01 + +### 문제 설명 + +

각각 부피가 A, B, C(1≤A, B, C≤200) 리터인 세 개의 물통이 있다. 처음에는 앞의 두 물통은 비어 있고, 세 번째 물통은 가득(C 리터) 차 있다. 이제 어떤 물통에 들어있는 물을 다른 물통으로 쏟아 부을 수 있는데, 이때에는 한 물통이 비거나, 다른 한 물통이 가득 찰 때까지 물을 부을 수 있다. 이 과정에서 손실되는 물은 없다고 가정한다.

+ +

이와 같은 과정을 거치다보면 세 번째 물통(용량이 C인)에 담겨있는 물의 양이 변할 수도 있다. 첫 번째 물통(용량이 A인)이 비어 있을 때, 세 번째 물통(용량이 C인)에 담겨있을 수 있는 물의 양을 모두 구해내는 프로그램을 작성하시오.

+ +### 입력 + +

첫째 줄에 세 정수 A, B, C가 주어진다.

+ +### 출력 + +

첫째 줄에 공백으로 구분하여 답을 출력한다. 각 용량은 오름차순으로 정렬한다.

+ diff --git a/백준/Gold/2251. 물통/물통.py b/백준/Gold/2251. 물통/물통.py new file mode 100644 index 0000000..7a9dcdb --- /dev/null +++ b/백준/Gold/2251. 물통/물통.py @@ -0,0 +1,89 @@ +import sys + +input=sys.stdin.readline + +A, B, C = map(int, input().split()) + +visited = set() +result = set() + +sizeA= A +sizeB = B +sizeC = C +total = C + + +def BFS(a, b, c): + """ + (a, b, c): 현재 A물통에 a, B물통에 b, C물통에 c 리터가 들어있는 상태. + """ + # 이미 방문했거나 범위를 벗어나면 중단 + if (a, b, c) in visited or a < 0 or b < 0 or c < 0 or a > sizeA or b > sizeB or c > sizeC: + return + + # 방문하지 않은 상태면 방문 처리 + visited.add((a, b, c)) + + # 디버깅 등을 위해 상태를 출력 (필요 없으면 주석 처리 가능) + # print(a, b, c) + + # 첫 번째 물통(A)이 비어있고, 전체 물 양이 처음과 같다면 C물통 양을 결과에 추가 + # (실제로 a+b+c는 항상 total이므로, 여기서는 `if a == 0:` 만 검사해도 됩니다) + if a == 0 and a + b + c == total: + result.add(c) + + # ---------------------- + # 1) B -> A (B물통에서 A물통으로 물 옮기기) + # ---------------------- + if b > 0 and a < sizeA: + # 실제로 옮길 수 있는 양 + transfer = min(b, sizeA - a) + BFS(a + transfer, b - transfer, c) + + # ---------------------- + # 2) A -> B (A물통에서 B물통으로 물 옮기기) + # ---------------------- + if a > 0 and b < sizeB: + transfer = min(a, sizeB - b) + BFS(a - transfer, b + transfer, c) + + # ---------------------- + # 3) C -> A (C물통에서 A물통으로 물 옮기기) + # ---------------------- + if c > 0 and a < sizeA: + transfer = min(c, sizeA - a) + BFS(a + transfer, b, c - transfer) + + # ---------------------- + # 4) A -> C (A물통에서 C물통으로 물 옮기기) + # ---------------------- + if a > 0 and c < sizeC: + transfer = min(a, sizeC - c) + BFS(a - transfer, b, c + transfer) + + # ---------------------- + # 5) C -> B (C물통에서 B물통으로 물 옮기기) + # ---------------------- + if c > 0 and b < sizeB: + transfer = min(c, sizeB - b) + BFS(a, b + transfer, c - transfer) + + # ---------------------- + # 6) B -> C (B물통에서 C물통으로 물 옮기기) + # ---------------------- + if b > 0 and c < sizeC: + transfer = min(b, sizeC - c) + BFS(a, b - transfer, c + transfer) + + # ---------------------- + # 6) B -> C (B물통에서 C물통으로 물 옮기기) + # ---------------------- + if b > 0 and c < sizeC: + transfer = min(b, sizeC - c) + BFS(a, b - transfer, c + transfer) + + +BFS(0, 0, C) +result = sorted(result) +for i in result: + print(i, end=' ') \ No newline at end of file