-
백준 2251번 물통카테고리 없음 2020. 2. 8. 19:56
https://www.acmicpc.net/problem/2251
2251번: 물통
각각 부피가 A, B, C(1≤A, B, C≤200) 리터인 세 개의 물통이 있다. 처음에는 앞의 두 물통은 비어 있고, 세 번째 물통은 가득(C 리터) 차 있다. 이제 어떤 물통에 들어있는 물을 다른 물통으로 쏟아 부을 수 있는데, 이때에는 한 물통이 비거나, 다른 한 물통이 가득 찰 때까지 물을 부을 수 있다. 이 과정에서 손실되는 물은 없다고 가정한다. 이와 같은 과정을 거치다보면 세 번째 물통(용량이 C인)에 담겨있는 물의 양이 변할 수도 있다.
www.acmicpc.net
<접근방법>
시작점과 도착점을 달리 하며 전부 탐색해봐야 되는 문제입니다
<코드>
#include <iostream> #include <algorithm> #include <queue> using namespace std; struct point { int a, b, c; }; int A, B, C; int visit[201][201][201]; priority_queue<int, vector<int>, greater<int>> pq; void bfs() { queue<point> q; q.push({0, 0, C}); while (!q.empty()) { point t = q.front(); q.pop(); int a = t.a; int b = t.b; int c = t.c; if (visit[a][b][c] == 1) continue; visit[a][b][c] = 1; if (a == 0) pq.push(c); //from a //to b if (a + b <= B) { q.push({ 0, b + a, c }); } else { q.push({ a - (B - b), B, c }); } //to c if (a + c <= C) { q.push({0, b, a + c}); } else { q.push({a - (C - c), b, C}); } //from b //to a if (b + a <= A) { q.push({b + a, 0, c}); } else { q.push({A, b - (A - a), c}); } //to c if (b + c <= C) { q.push({ a, 0, b + c }); } else { q.push({ a, b - (C - c) , C }); } //from C //to a if (a + c <= A) { q.push({a + c, b, 0}); } else { q.push({ A, b, c - (A - a)}); } //to b if (b + c <= B) { q.push({a, b + c, 0}); } else { q.push({a, B, c - (B - b)}); } } } int main() { cin >> A >> B >> C; bfs(); while (!pq.empty()) { cout << pq.top() << '\n'; pq.pop(); } return 0; }
<느낀 점>
-if else 조심해서 쓰자
반응형