Baekjoon/BFS
[백준 2251] 물통 (BFS) (C/C++)
워니-
2019. 12. 2. 11:46
#include <stdio.h> #include <iostream> #include <queue> #include <vector> #include <algorithm> using namespace std; int A, B, C; int visited[201][201][201]; int check[201]; queue<int> q; void BFS(int nowA, int nowB, int nowC) { visited[nowA][nowB][nowC] = 1; q.push(nowA); q.push(nowB); q.push(nowC); // C의 용량 체크 check[nowC] = 1; while(!q.empty()) { nowA = q.front(); q.pop(); nowB = q.front(); q.pop(); nowC = q.front(); q.pop(); for(int i = 1; i <= 6; i++) { int newA = nowA; int newB = nowB; int newC = nowC; // A->B if(i == 1) { if(nowA + nowB > B) { newA = nowA + nowB - B; newB = B; } else { newB = nowA + nowB; newA = 0; } } // B->A else if(i == 2) { if(nowB + nowA > A) { newB = nowB + nowA - A; newA = A; } else { newA = nowB + nowA; newB = 0; } } // A->C else if(i == 3) { if(nowA + nowC > C) { newA = nowA + nowC - C; newC = C; } else { newC = nowA + nowC; newA = 0; } } // C->A else if(i == 4) { if(nowC + nowA > A) { newC = nowC + nowA - A; newA = A; } else { newA = nowC + nowA; newC = 0; } } // B->C else if(i == 5) { if(nowB + nowC > C) { newB = nowB + nowC - C; newC = C; } else { newC = nowB + nowC; newB = 0; } } // C->B else if(i == 6) { if(nowC + nowB > B) { newC = nowC + nowB - B; newB = B; } else { newB = nowC + nowB; newC = 0; } } if(visited[newA][newB][newC] == 0) { visited[newA][newB][newC] = 1; q.push(newA); q.push(newB); q.push(newC); if(newA == 0 && check[newC] == 0) { check[newC] = 1; } } } } } int main(void) { // freopen("B2251_input.txt", "r", stdin); cin >> A >> B >> C; BFS(0, 0, C); for(int i = 0; i <= 200; i++) { if(check[i] == 1) { cout << i << " "; } } return 0; } | cs |