✔ 문제 링크
BOJ 5014: 스타트링크
✔ 문제해결전략
- 그래프 탐색
- 1차원에서의 BFS(Breadth First Search)
✔ 해결과정
✔ 정답 Code
#include <bits/stdc++.h>
using namespace std;
int stair[1000001];
int main(void) {
ios::sync_with_stdio(0);
cin.tie(0);
int f, s, g, u ,d;
queue<int> q;
cin >> f >> s >> g >> u >> d;
fill( stair, stair + 1000001, -1);
stair[s] = 0;
q.push(s);
while(!q.empty()) {
int cur = q.front();
q.pop();
for(int new_: {cur-d, cur+u}) {
if(new_<1 || new_>f) continue;
if(stair[new_] != -1) continue;
stair[new_] = stair[cur] + 1;
q.push(new_);
}
}
if(stair[g]==-1) {
cout << "use the stairs";
return 0;
}
cout << stair[g] << '\n';
}