boj)5014 - 스타트링크
2020. 11. 2. 13:36ㆍPS/boj
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
import java.io.*;
import java.util.*;
public class boj_5014 {
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static StringTokenizer st;
static Queue<int[]> q = new LinkedList<>();
static boolean[] v;
static int[] a;
static int F, S, G, U, D, ans;
public static void main(String[] args) throws IOException {
input();
solve();
if (ans == -1) System.out.println("use the stairs");
else System.out.println(ans);
}
static void input() throws IOException {
st = new StringTokenizer(br.readLine());
F = Integer.parseInt(st.nextToken());
S = Integer.parseInt(st.nextToken());
G = Integer.parseInt(st.nextToken());
U = Integer.parseInt(st.nextToken());
D = Integer.parseInt(st.nextToken());
v = new boolean[F + 1];
a = new int[F + 1];
ans = -1;
}
static void solve() {
q.offer(new int[]{S, 0});
v[S] = true;
while (!q.isEmpty()) {
int[] now = q.poll();
int floor = now[0];
int cnt = now[1];
if (floor == G) { // 목표층 도달
ans = cnt;
break;
}
int up = floor + U;
int down = floor - D;
if (up <= F && !v[up]) {
v[up] = true;
q.offer(new int[]{up, cnt + 1});
}
if (down >= 1 && !v[down]) {
v[down] = true;
q.offer(new int[]{down, cnt + 1});
}
}
}
}
|
cs |
- 현재 층부터 탐색 시작
- 목표 층에 도달시 break;
- 목표 층이 아닐 경우 up, down 두 가지 경우로 분류해서 큐가 빌때까지 offer
'PS > boj' 카테고리의 다른 글
boj)1707 - 이분그래프 (0) | 2020.11.03 |
---|---|
boj)11724 - 연결 요소의 개수 (0) | 2020.11.02 |
boj)2146 - 다리 만들기 (0) | 2020.11.02 |
boj)13023 - ABCDE (0) | 2020.10.29 |
boj)2206 - 벽 부수고 이동하기 (0) | 2020.10.24 |