[BOJ] 13549๋ฒˆ | ์ˆจ๋ฐ”๊ผญ์งˆ 3 (C++)

2023. 12. 6. 23:00ใ†Coding Test/BOJ

๐Ÿ”—๋ฌธ์ œ ๋ณด๋Ÿฌ๊ฐ€๊ธฐ
 

13549๋ฒˆ: ์ˆจ๋ฐ”๊ผญ์งˆ 3

์ˆ˜๋นˆ์ด๋Š” ๋™์ƒ๊ณผ ์ˆจ๋ฐ”๊ผญ์งˆ์„ ํ•˜๊ณ  ์žˆ๋‹ค. ์ˆ˜๋นˆ์ด๋Š” ํ˜„์žฌ ์  N(0 โ‰ค N โ‰ค 100,000)์— ์žˆ๊ณ , ๋™์ƒ์€ ์  K(0 โ‰ค K โ‰ค 100,000)์— ์žˆ๋‹ค. ์ˆ˜๋นˆ์ด๋Š” ๊ฑท๊ฑฐ๋‚˜ ์ˆœ๊ฐ„์ด๋™์„ ํ•  ์ˆ˜ ์žˆ๋‹ค. ๋งŒ์•ฝ, ์ˆ˜๋นˆ์ด์˜ ์œ„์น˜๊ฐ€ X์ผ ๋•Œ

www.acmicpc.net

 

๐Ÿง‘โ€๐Ÿ’ป ํ’€์ด ๊ณผ์ •

 

์ตœ๋‹จ ๊ฒฝ๋กœ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์—ฐ์Šต ์ค‘์— ์žˆ์–ด, ์ €๋Š” ๋‹ค์ต์ŠคํŠธ๋ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜์œผ๋กœ ํ’€์—ˆ์Šต๋‹ˆ๋‹ค. (๋น„์šฉ์— ์Œ์ˆ˜๊ฐ€ ์—†๊ธฐ ๋•Œ๋ฌธ)

์‹œ์ž‘ ์œ„์น˜๋Š” N, ๋„์ฐฉ ์œ„์น˜๋Š” K๋กœ ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค.

 

  1. N์—์„œ i๋ฒˆ์งธ ๋…ธ๋“œ๋กœ ๊ฐ€๋Š” ๋ฐ์— ๋“œ๋Š” ์ตœ์†Œ ์‹œ๊ฐ„์„ ์ €์žฅํ•˜๋Š” ๋ฐฐ์—ด, times๋ฅผ ์„ ์–ธํ•ฉ๋‹ˆ๋‹ค.
    ๋ฒ”์œ„๊ฐ€ 0 ~ 100,000์ด๋‹ˆ, ์ด ๋ฒ”์œ„๋ฅผ ํฌํ•จํ•  ์ˆ˜ ์žˆ๋Š” ๋งŒํผ์˜ ํฌ๊ธฐ๋กœ ์„ ์–ธ ํ›„, ๋ฌดํ•œ๋Œ€ ๊ฐ’์œผ๋กœ ์ดˆ๊ธฐํ™”ํ•ฉ๋‹ˆ๋‹ค.
  2. times[N] = 0 ์œผ๋กœ ์ดˆ๊ธฐํ™” ํ›„, <์‹œ๊ฐ„, ๋…ธ๋“œ> ์ •๋ณด๋ฅผ ๋‹ด๋Š” ์šฐ์„ ์ˆœ์œ„ ํ์— ์‹œ์ž‘์  ์ •๋ณด <0, N>์„ ๋„ฃ์Šต๋‹ˆ๋‹ค.
  3. ์ด๋™ ๊ทœ์น™์— ๋”ฐ๋ผ -1, +1, *2๋ฅผ ์ ์šฉํ•œ ๋‹ค์Œ ์œ„์น˜(next)๋ฅผ ๊ณ„์‚ฐํ•˜๊ณ , ํ˜„์žฌ ์‹œ๊ฐ„์—์„œ ๋‹ค์Œ ์œ„์น˜๋กœ ์ด๋™ํ•˜๋Š” ๋ฐ ํ•„์š”ํ•œ ์‹œ๊ฐ„(0 ๋˜๋Š” 1)์„ ๋”ํ•œ nextTime์„ ๊ณ„์‚ฐํ•ฉ๋‹ˆ๋‹ค.
    • nextTime์ด ๊ธฐ์กด์— next ๋…ธ๋“œ๋กœ ์ด๋™ํ•˜๋Š” ์‹œ๊ฐ„๋ณด๋‹ค ๋งŽ๋‹ค๋ฉด, ๋„˜์–ด๊ฐ‘๋‹ˆ๋‹ค.
  4. ๊ธฐ์กด์— next ๋…ธ๋“œ๋กœ ์ด๋™ํ•˜๋Š” ๋ฐ ๊ฑธ๋ฆฌ๋Š” ์‹œ๊ฐ„ vs ํ˜„์žฌ ๋…ธ๋“œ๋ฅผ ๊ฒฝ์œ ํ•ด์„œ next ๋…ธ๋“œ๋กœ ๊ฐ€๋Š” ์‹œ๊ฐ„ ์ค‘์— ์ž‘์€ ๊ฒƒ์„ times[next]์— ๋„ฃ๊ณ , ํ์— ์ด ์ •๋ณด๋ฅผ ๋„ฃ์Šต๋‹ˆ๋‹ค.
  5. ๋งŒ์•ฝ next == K๋ผ๋ฉด, times[K] = min(times[K], nextTime) ์œผ๋กœ ๊ฐฑ์‹ ํ•ด์ค๋‹ˆ๋‹ค.
  6. ํ๊ฐ€ ๋นŒ ๋•Œ๊นŒ์ง€, 3 ~ 5 ๊ณผ์ •์„ ๋ฐ˜๋ณตํ•ฉ๋‹ˆ๋‹ค.

 

โœ๏ธ ์†Œ์Šค ์ฝ”๋“œ ๋ฐ ๊ฒฐ๊ณผ

#include <iostream>
#include <vector>
#include <queue>

using namespace std;
#define FAST_IO ios::sync_with_stdio(false); cout.tie(NULL); cin.tie(NULL);
#define MAX 100000
#define INF 1000000000

int Calculate(int node, int i) {
    switch(i) {
        case 0: return node - 1;
        case 1: return node + 1;
        case 2: return node * 2;
    }
    return 0;
}

int FindMinTime(vector<int>& times, int start, int K) {
    priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
    // <์ตœ์†Œ ์‹œ๊ฐ„, ๋…ธ๋“œ>
    
    times[start] = 0;
    pq.emplace(0, start);

    while (!pq.empty()) {
        pair<int, int> current = pq.top();
        int time = current.first;
        int node = current.second;
        pq.pop();

        for (int i = 0; i < 3; i++) {
            int next = Calculate(node, i);

            if (next < 0 or next > MAX or times[next] < time)
                continue;

            bool isTeleport = (i == 2);
            int nextTime = time + !isTeleport;

            if (next == K)
                times[K] = min(nextTime, times[K]);

            if (nextTime < times[next]) {
                times[next] = nextTime;
                pq.emplace(nextTime, next);
            }
        }
    }

    return times[K];
}

int main() {
    FAST_IO
    int N, K;
    cin >> N >> K;

    vector<int> times(MAX + 1, INF);
    int answer = FindMinTime(times, N, K);
    cout << answer;
    return 0;
}

 

 

728x90
๋ฐ˜์‘ํ˜•