백준 5014번: 스타트링크

Seungil Kim·2021년 10월 19일
0

PS

목록 보기
59/206

스타트링크

백준 5014번: 스타트링크

아이디어

간단한 bfs 문제다. 건물 범위를 벗어나지 않도록(1층부터 f층까지) 조심하고 한 번도 층에 도달한 적이 없거나, 더 짧은 시간 안에 도달할 수 있는 경우 큐에 집어넣는다.

코드

#include <bits/stdc++.h>

using namespace std;

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    // max->f start->s destination->g
    int f, s, g, u, d;
    queue<int> q;
    cin >> f >> s >> g >> u >> d;
    int elev[f+1] = {};
    elev[s] = 1;
    q.push(s);
    while (!q.empty()) {
        int cur = q.front();
        if (cur == g) {
            break;
        }
        q.pop();
        if (cur+u <= f && (elev[cur+u] == 0 || elev[cur+u] > elev[cur] + 1)) {
            elev[cur+u] = elev[cur] + 1;
            q.push(cur+u);
        }
        if (cur-d >= 1 && (elev[cur-d] == 0 || elev[cur-d] > elev[cur] + 1)) {
            elev[cur-d] = elev[cur] + 1;
            q.push(cur-d);
        }
    }
    
    if (elev[g] == 0) {
        cout << "use the stairs";
    }
    else {
        cout << elev[g] - 1;
    }
    return 0;
}

여담

범위 조심. 쉬운 문제 푸니까 힐링된다.

profile
블로그 옮겼어용 https://ks1ksi.io/

0개의 댓글