[BOJ] 5014 스타트링크

Time Lapse :6min 28sec

5014.cpp

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
#include <stdio.h>
#include <stdlib.h>
#include <queue>
using namespace std;
bool stair[1000001] = {false,};
int F, S, G, U, D, t;
int main(void){
queue<int> q;
scanf("%d %d %d %d %d",&F,&S,&G,&U,&D);
stair[S] = true;
q.push(S);
if(S==G) {
printf("0");
exit(0);
}
while(!q.empty()){
int size = q.size();
for(int i = 0; i < size; ++i){
int s = q.front();
q.pop();
if(s+U<=F&&!stair[s+U]){
if(s+U==G) {printf("%d",t+1); exit(0);}
stair[s+U] = true;
q.push(s+U);
}
if(s-D>=1&&!stair[s-D]){
if(s-D==G) {printf("%d",t+1); exit(0);}
stair[s-D] = true;
q.push(s-D);
}
}
t++;
}
printf("use the stairs");
}
Author: Song Hayoung
Link: https://songhayoung.github.io/2020/07/30/PS/BOJ/5014/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.