Recent Posts
Notice
No Rules Rules
스타트링크 (feat. 백준, 5014번) 본문
728x90
반응형
스타트링크
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
반응형
// woohyeon.kim
// kim519620.tistory.com
#include <iostream>
#include <string.h>
#include <queue>
using namespace std;
int ans[1000001];
bool visit[1000001];
int main(){
ios::sync_with_stdio(false), cin.tie(NULL);
register int F, S, G, U, D;
cin >> F >> S >> G >> U >> D;
memset(ans, -1, sizeof(ans[0]) * (F + 1));
memset(visit, false, F + 1);
queue<int> q;
q.push(S);
ans[S] = 0;
visit[S] = true;
while(!q.empty()){
auto p = q.front(); q.pop();
if(p + U <= F && !visit[p + U])
visit[p + U] = true, q.push(p + U), ans[p + U] = ans[p] + 1;
if(p - D >= 1 && !visit[p - D])
visit[p - D] = true, q.push(p - D), ans[p - D] = ans[p] + 1;
}
if(ans[G] == -1)
cout << "use the stairs";
else
cout << ans[G];
return 0;
}
// *&)*@*
- 강호가 있는 위치(S층)에서 스타트링크가 있는 위치(G층) 으로는 위(U층) 또는 아래(D층) 로만 움직일 수 있습니다.
- 따라서 S+U와 S-D에 대해서 너비 우선 탐색인 bfs로 모든 탐색을 마쳤을 때, G층의 인덱스에 값이 있다면 그 값을, 없다면 "use the stairs" 를 출력하는 문제입니다.
728x90
반응형
'생활 > 코테' 카테고리의 다른 글
빙산 (feat. 백준, 2573번) (0) | 2022.09.14 |
---|---|
안전 영역 (feat. 백준, 2468번) (0) | 2022.09.14 |
촌수계산 (feat. 백준, 2644번) (0) | 2022.09.14 |
파이프 옮기기 1 (feat. 백준, 17070번) (0) | 2022.09.08 |
괄호 추가하기 (feat. 백준, 16637번) (0) | 2022.09.08 |
Comments