문제) 백준 - BFS - 물통
https://www.acmicpc.net/problem/2251
2251번: 물통
각각 부피가 A, B, C(1≤A, B, C≤200) 리터인 세 개의 물통이 있다. 처음에는 앞의 두 물통은 비어 있고, 세 번째 물통은 가득(C 리터) 차 있다. 이제 어떤 물통에 들어있는 물을 다른 물통으로 쏟아 부
www.acmicpc.net
a, b, c의 양을 bfs를 통해 탐색합니다. 모든 경우(물 옮겨 담기)를 시행하면서, a가 0일 경우 임의의 배열에 담아 결과를 출력합니다.
C++ 소스코드)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
while (!q.empty()) { | |
int a = q.front().first; | |
int b = q.front().second.first; | |
int c = q.front().second.second; | |
q.pop(); | |
if (visited[a][b][c]) continue; | |
visited[a][b][c] = true; | |
if (a == 0) ans.push_back(c); | |
//a->b | |
if (a + b > B) | |
q.push({ a + b - B, {B, c} }); | |
else | |
q.push({ 0, {a + b, c} }); | |
//a->c | |
if (a + c > C) | |
q.push({ a + c - C, {b, C} }); | |
else | |
q.push({ 0, {b, a + c} }); | |
//b->a | |
if (a + b > A) | |
q.push({ A, {a + b - A, c} }); | |
else | |
q.push({ a + b, {0, c} }); | |
//b->c | |
if (c + b > C) | |
q.push({ a, {b + c - C, C} }); | |
else | |
q.push({ a, {0, b + c} }); | |
//c->a | |
if (a + c > A) | |
q.push({ A, {b , c + a - A} }); | |
else | |
q.push({ a + c, {b, 0} }); | |
//c->b | |
if (c + b > B) | |
q.push({ a , {B, c + b - B} }); | |
else | |
q.push({ a, {c + b, 0} }); | |
} |
Full Code)
GitHub - Chocochip101/BOJ_Solution: BOJ Solutions
BOJ Solutions. Contribute to Chocochip101/BOJ_Solution development by creating an account on GitHub.
github.com
반응형
'PS(Problem Solving) > 백준_BOJ' 카테고리의 다른 글
[백준] 17471번 - 게리맨더링 (파이썬) 문제 및 풀이 (0) | 2022.01.04 |
---|---|
[백준] 1110번 - 더하기 사이클 (파이썬) 문제 및 풀이 (0) | 2022.01.01 |
[백준] 2533번 - 사회망 서비스(SNS) (C++) 문제 및 풀이 (0) | 2021.12.30 |
[백준] 10826번 - 피보나치 수 4 (Python) 문제 및 풀이 (0) | 2021.12.30 |
[백준] 13549번 - 숨바꼭질 3 (C++) 문제 및 풀이 (0) | 2021.12.28 |
댓글