# 생각
1차원 BFS로 해결 가능하다
# 전체 코드
#include <bits/stdc++.h>
using namespace std;
int dist[1000002];
int F, S, G, U, D;
queue<int> q;
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> F >> S >> G >> U >> D;
fill(dist, dist + F + 1, -1);
dist[S] = 0;
q.push(S);
while (!q.empty())
{
auto cur = q.front(); q.pop();
for (int dir = 0; dir < 2; dir++)
{
int nx;
if (dir == 0) nx = cur + U;
else nx = cur - D;
if (nx <= 0 || nx > F || dist[nx] != -1) continue;
dist[nx] = dist[cur] + 1;
q.push(nx);
}
// 위의 for문과 똑같은 동작을 하는 ranged for문
//for (auto nxt : { cur + U,cur - D })
//{
// if (nxt <= 0 || nxt > F || dist[nxt] != -1) continue;
// dist[nxt] = dist[cur] + 1;
// q.push(nxt);
//}
}
if (dist[G] == -1) cout << "use the stairs";
else cout << dist[G];
}
https://www.acmicpc.net/problem/5014
5014번: 스타트링크
첫째 줄에 F, S, G, U, D가 주어진다. (1 ≤ S, G ≤ F ≤ 1000000, 0 ≤ U, D ≤ 1000000) 건물은 1층부터 시작하고, 가장 높은 층은 F층이다.
www.acmicpc.net
'Algorithm > BOJ' 카테고리의 다른 글
[BOJ] 1629_곱셈 with cpp (0) | 2022.01.25 |
---|---|
[BOJ] 5427_불 with cpp (0) | 2022.01.25 |
[BOJ] 6593_상범 빌딩 with cpp (0) | 2022.01.23 |
[BOJ] 2468_안전 영역.cpp (0) | 2022.01.23 |
[BOJ] 2667_단지번호붙이기 with cpp (0) | 2022.01.19 |