Post

[C++]백준 5014번 스타트링크

[C++]백준 5014번 스타트링크

📌문제 링크


https://www.acmicpc.net/problem/5014

스크린샷 2025-01-15 오후 10 18 25

📌문제 설명


bfs로 간단하게 해결 가능합니다. 각 층에서 U층, D층 만큼 이동 가능할 때의 값을 계속 저장한 후 G에 도착했을때 값을 출력하면 됩니다.

📌코드


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
int F, S, G, U, D, tower[1000001];
using namespace std;

void input(){
    cin >> F >> S >> G >> U >> D;
}

void bfs(){
    for(int i = 0; i <= F; i++){
        tower[i] = -1;
    }
    queue<pair<int, int>> q;
    q.push({S, 0});
    tower[S] = 0;
    while(!q.empty()){
        auto [idx, cnt] = q.front();
        q.pop();
        if(idx == G){
            cout << cnt;
            exit(0);
        }
        if(idx + U <= F && U != 0){
            if(tower[idx + U] == -1){
                tower[idx + U] = cnt + 1;
                q.push({idx + U, cnt + 1});
            }
        }
        if(idx - D >= 1 && D != 0){
            if(tower[idx - D] == -1){
                tower[idx - D] = cnt + 1;
                q.push({idx - D, cnt + 1});
            }
        }
    }
    cout << "use the stairs\n";
}

void solve(){
    input();
    bfs();
}

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    solve();
    return 0;
}
This post is licensed under CC BY 4.0 by the author.