https://www.acmicpc.net/problem/5014
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
int f, s, g, u, d;
int dp[1000001];
int bfs() {
queue<int> q;
q.push(s);
dp[s] = 1;
while (!q.empty()) {
int now = q.front();
q.pop();
if (now == g) {
return dp[now];
}
int nextUp = now + u;
int nextDown = now - d;
if (nextUp <= f && !dp[nextUp]) {
dp[nextUp] = dp[now] + 1;
q.push(nextUp);
}
if (nextDown >= 1 && !dp[nextDown]) {
dp[nextDown] = dp[now] + 1;
q.push(nextDown);
}
}
return -1;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> f >> s >> g >> u >> d;
int result = bfs();
if (result == -1) {
cout << "use the stairs";
}
else cout << result - 1;
return 0;
}
2차원 배열만 풀다가 1차원으로 접근하니깐 까다로웠다. 문제를 골고루 풀어야겠다😅
문제 편식하지 마세요^^ 코테에서 된통당해요;;;;