Little Jay

[C++]백준 5014번 - 면접에 늦었다 본문

알고리즘/BOJ

[C++]백준 5014번 - 면접에 늦었다

Jay, Lee 2022. 3. 14. 13:42

BFS 구현으로 풀 수 있는 문제

#include <bits/stdc++.h>
#define endl '\n'
using namespace std;

int dist[1000001];
bool check[1000001];
int q[1000000];
int f, s, g, u, d;

/*
* bfs 조건 분기는 0 < RESULT <= F이다
*/

int main() {

	ios::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);

	cin >> f >> s >> g >> u >> d;

    int begin = 0;
    int end = 0;
    q[end++] = s;
    dist[s] = 0;
    check[s] = true;
    while (begin < end) {
        int now = q[begin++];
        if (now + u <= f && check[now + u] == false) {
            q[end++] = now + u;
            dist[now + u] = dist[now] + 1;
            check[now + u] = true;
        }
        if (now - d >= 1 && check[now - d] == false) {
            q[end++] = now - d;
            dist[now - d] = dist[now] + 1;
            check[now - d] = true;
        }
    }
    if (check[g] == false) {
        cout << "use the stairs" << endl;
    }
    else {
        cout << dist[g] << endl;
    }


	return 0;

}
Comments