Notice
Recent Posts
Recent Comments
Link
Little Jay
[C++]백준 5014번 - 면접에 늦었다 본문
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;
}'알고리즘 > BOJ' 카테고리의 다른 글
| [C++] 백준 6679번 - 싱기한 네자리 숫자 (0) | 2022.03.20 |
|---|---|
| [C++] 백준 1655번 - 가운데를 말해요 (0) | 2022.03.14 |
| [C++] 백준 11055번 - 가장 큰 증가 부분 수열 (0) | 2022.02.21 |
| [C++] 백준 9461번 - 파도반 수열 (0) | 2022.02.21 |
| [C++] 백준 1236 - 성지키기 (0) | 2022.02.19 |
Comments