-
Notifications
You must be signed in to change notification settings - Fork 1
/
Baekjoon12761.java
77 lines (59 loc) · 2.24 KB
/
Baekjoon12761.java
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package algo.Algorithms;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;
/**
* https://www.acmicpc.net/problem/12761
* 백준 12761번 돌다리
*/
public class Baekjoon12761 {
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static int A, B, N, M;
static int[] visited = new int[100001];
static int[] dx ;
public static void main(String[] args) throws IOException {
StringTokenizer st = new StringTokenizer(br.readLine());
A = Integer.parseInt(st.nextToken());
B = Integer.parseInt(st.nextToken());
N = Integer.parseInt(st.nextToken());
M = Integer.parseInt(st.nextToken());
Arrays.fill(visited, Integer.MAX_VALUE);
dx = new int[]{A, B, 1};
solution();
}
private static void solution() {
Queue<Integer> queue = new LinkedList<>();
visited[N] = 0;
queue.add(N);
while (!queue.isEmpty()) {
int current = queue.poll();
if(current==M){
System.out.println(visited[M]);
return;
}
int nextPosition = 0;
for (int i = 0; i < 2; i++) {
nextPosition = current*dx[i];
if( nextPosition < 0 || nextPosition>100000 || visited[nextPosition]<=visited[current]+1) continue;
visited[nextPosition] = visited[current]+1;
queue.add(nextPosition);
}
for (int i = 0; i < 3; i++) {
nextPosition = current + dx[i];
if( nextPosition < 0 || nextPosition>100000 || visited[nextPosition]<=visited[current]+1) continue;
visited[nextPosition] = visited[current]+1;
queue.add(nextPosition);
}
for (int i = 0; i < 3; i++) {
nextPosition = current - dx[i];
if( nextPosition < 0 || nextPosition>100000 || visited[nextPosition]<=visited[current]+1) continue;
visited[nextPosition] = visited[current]+1;
queue.add(nextPosition);
}
}
}
}