BOJ : https://www.acmicpc.net/problem/5014
github : https://github.com/junho0956/Algorithm/blob/master/5014/5014/%EC%86%8C%EC%8A%A4.cpp
간단한 BFS 문제입니다.
주의해야할 점이 있다면 건물은 1층부터 F 층까지 있다는 점으로
범위가 벗어나면 갈 수 없다는 점입니다
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
|
#include <iostream>
#include <queue>
using namespace std;
queue<pair<int, int> > q;
bool visit[1000001];
int main() {
ios::sync_with_stdio(0), cin.tie(0);
bool check = false;
int f, s, g, u, d;
cin >> f >> s >> g >> u >> d;
q.push({ s,0 });
while (!q.empty()) {
int now = q.front().first;
int cnt = q.front().second;
q.pop();
if (now > f) continue;
if (now < 1) continue;
if (visit[now]) continue;
visit[now] = true;
if (now == g) {
cout << cnt;
check = true;
break;
}
if (u)q.push({ now + u, cnt + 1 });
if (d)q.push({ now - d, cnt + 1 });
}
if (!check) cout << "use the stairs";
return 0;
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter
|
'algorithm > BOJ' 카테고리의 다른 글
BOJ 2661번 좋은수열 (0) | 2020.01.31 |
---|---|
BOJ 1759번 암호 만들기 (0) | 2020.01.31 |
BOJ 3108번 로고 (0) | 2020.01.31 |
BOJ 2186번 문자판 (0) | 2020.01.27 |
BOJ 2251번 물통 (0) | 2020.01.24 |