Henu
개발냥발
Henu
전체 방문자
오늘
어제
  • 분류 전체보기 (411)
    • DevOps (52)
      • Kubernetes (19)
      • Docker (14)
      • AWS (3)
      • Nginx (4)
      • Linux (4)
      • ArgoCD (1)
      • CN (2)
      • NATS (0)
      • Git (5)
    • Back-End (30)
      • Django (18)
      • Spring (5)
      • JPA (1)
      • MSA (5)
    • CS (87)
      • SystemSoftware (20)
      • OS (25)
      • Computer Architecture (16)
      • Network (23)
      • Database (2)
    • Lang (21)
      • Java (9)
      • Python (4)
      • C# (8)
    • Life (12)
    • 블록체인 (2)
    • Algorithm (204)
      • BOJ (160)
      • 프로그래머스 (19)
      • LeetCode (4)
      • SWEA (1)
      • 알고리즘 문제 해결 전략 (8)
      • DS, algorithms (7)
      • Checkio (5)
    • IT (2)

블로그 메뉴

  • GitHub
  • 글쓰기
  • 관리자

공지사항

  • Free!

인기 글

태그

  • 다이나믹 프로그래밍
  • boj
  • DFS
  • Network
  • 백트래킹
  • docker
  • BFS
  • 프로그래머스
  • django
  • Kubernetes

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
Henu

개발냥발

Algorithm/BOJ

[백준 14867] 물통 (C++)

2022. 1. 19. 09:41
 

14867번: 물통

표준 입력으로 물통 A의 용량을 나타내는 정수 a(1 ≤ a < 100,000), 물통 B의 용량을 나타내는 정수 b(a < b ≤ 100,000), 최종 상태에서 물통 A에 남겨야 하는 물의 용량을 나타내는 정수 c(0 ≤ c ≤ a), 최

www.acmicpc.net

 

방문 여부를 체크하는 BFS 문제이다.

물의 용량이 100000, 100000 이므로 배열로 체크하진 못하고, map 자료형을 사용해서 체크할 수 있다.

 

bfs를 수행하면서 큐에 상태를 넣기 위한 조건이 있다.

  • A에 물을 가득 채우기
  • B에 물을 가득 채우기
  • A를 비우기
  • B를 비우기
  • A->B로 물 이동
  • B->A로 물이동

현재 상태로부터 총 6가지의 상태로 변화할 수 있다.

 

하지만 bfs의 특성상 다음 상태가 이미 나온적이 있는 상태라면 해당 상태부터 다시 탐색할 필요가 없기 때문에 넘어간다.

 

A->B, B->A인 경우 문제에서 주어진대로 구현을 해서 물의 용량을 결정해주면 된다.

 

 

#include <iostream>
#include <vector>
#include <queue>
#include <map>
#include <algorithm>
#include <cstring>
#define fasti ios_base::sync_with_stdio(false); cin.tie(0);
#define fastio ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define INF 1e9+7
#define pii pair<int, int>

typedef long long ll;
// typedef pair<int, int> pii;

using namespace std;

struct State{
    pii st;
    int cnt;
};

int A, B, C, D;
map<pii, int> visited;

void input(){
    cin >> A >> B >> C >> D;
}

void bfs(){
    int answer = INF;
    
    pii start = {0, 0};
    queue<State> que;
    que.push({start, 0});
    visited[start] = 1;
    
    while(!que.empty()){
        pii now = que.front().st;
        int now_cnt = que.front().cnt;
        que.pop();
        pii _next;
        
        if(now.first == C && now.second == D){
            answer = min(answer, now_cnt);
        }
        
        // A물통을 가득 채우는 경우
        _next = {A, now.second};
        if(!visited[_next]){
            visited[_next] = 1;
            que.push({_next, now_cnt+1});
        }
        
        // B물통을 가득 채우는 경우
        _next = {now.first, B};
        if(!visited[_next]){
            visited[_next] = 1;
            que.push({_next, now_cnt+1});
        }
        
        // A를 모두 버리는 경우
        _next = {0, now.second};
        if(!visited[_next]){
            visited[_next] = 1;
            que.push({_next, now_cnt+1});
        }
        
        // B를 모두 버리는 경우
        _next = {now.first, 0};
        if(!visited[_next]){
            visited[_next] = 1;
            que.push({_next, now_cnt+1});
        }
        
        // A -> B로 이동
        int b_left = B - now.second;
        if(now.first >= b_left) _next = {now.first - b_left, B};
        else _next = {0, now.first + now.second};
        if(!visited[_next]){
            visited[_next] = 1;
            que.push({_next, now_cnt+1});
        }
        
        // B -> A로 이동
        int a_left = A - now.first;
        if(now.second >= a_left) _next = {A, now.second - a_left};
        else _next = {now.first + now.second, 0};
        if(!visited[_next]){
            visited[_next] = 1;
            que.push({_next, now_cnt+1});
        }
    }
    
    if(answer == INF) cout << -1;
    else cout << answer;
}

void solve(){
    bfs();
}

int main(){
    input();
    solve();
    
    return 0;
}

 

'Algorithm > BOJ' 카테고리의 다른 글

[백준 17135] 캐슬 디펜스 (C++)  (0) 2022.06.07
[백준 16637] 괄호 추가하기 (C++)  (0) 2022.04.24
[백준 5547] 일루미네이션 (C++)  (0) 2022.01.19
[백준 2836] 수상 택시 (C++)  (0) 2022.01.12
[백준 4095] 최대 정사각형 (C++)  (0) 2022.01.11
    'Algorithm/BOJ' 카테고리의 다른 글
    • [백준 17135] 캐슬 디펜스 (C++)
    • [백준 16637] 괄호 추가하기 (C++)
    • [백준 5547] 일루미네이션 (C++)
    • [백준 2836] 수상 택시 (C++)

    티스토리툴바