백준 문제풀이/GOLD

[백준 / BOJ / GOLD 5] 5014 번 : 스타트링크

지나가던 개발자 2021. 3. 24. 22:34
반응형

www.acmicpc.net/problem/5014

 

5014번: 스타트링크

첫째 줄에 F, S, G, U, D가 주어진다. (1 ≤ S, G ≤ F ≤ 1000000, 0 ≤ U, D ≤ 1000000) 건물은 1층부터 시작하고, 가장 높은 층은 F층이다.

www.acmicpc.net

문제 접근법

  • S층에서 G층까지 가는 최소 버튼수를 구해야한다.
  • 최단거리를 구할 때 사용할 수 있는 BFS를 사용해도 되는지 체크해본다.
  • 최대 백만번을 초과하지 않으므로 BFS를 충분히 사용가능하다.

아래는 코드입니다.

 

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
54
55
56
57
58
59
#include <iostream>
#include <queue>
 
using namespace std;
 
bool check[1000001];
 
int F, S, G, U, D;
 
bool InRange(int x)
{
    return x > 0 && x <= F;
}
 
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
 
    cin >> F >> S >> G >> U >> D;
 
    queue<pair<intint>> q;
    q.push(make_pair(S, 0));
 
    check[0= true;
    check[S] = true;
 
    while (!q.empty())
    {
        int x = q.front().first;
        int count = q.front().second;
 
        if (x == G)
        {
            cout << count << "\n";
            return 0;
        }
        q.pop();
 
        for (int i = 0; i < 2; i++)
        {
            int nx = 0;
            if (i == 0)
                nx = x + U;
            else
                nx = x - D;
 
            if (InRange(nx) && !check[nx])
            {
                check[nx] = true;
                q.push(make_pair(nx, count + 1));
            }
        }
    }
 
    cout << "use the stairs\n";
 
    return 0;
}
cs
반응형