-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathboj_16928.java
More file actions
70 lines (59 loc) · 1.88 KB
/
boj_16928.java
File metadata and controls
70 lines (59 loc) · 1.88 KB
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
package dfs_bfs;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;
//참고: https://jumping-to-the-water.tistory.com/117
public class boj_16928 {
static int N,M;
static int[] arr;
public static void main(String[] args) throws IOException {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st=new StringTokenizer(br.readLine());
N=Integer.parseInt(st.nextToken());
M=Integer.parseInt(st.nextToken());
arr=new int[101];
//1~100 초기화
for(int i=1;i<arr.length;i++){
arr[i]=i;
}
for(int i=0;i<N;i++){
st=new StringTokenizer(br.readLine());
int x=Integer.parseInt(st.nextToken());
int y=Integer.parseInt(st.nextToken());
arr[x]=y;
}
for(int i=0;i<M;i++){
st=new StringTokenizer(br.readLine());
int x=Integer.parseInt(st.nextToken());
int y=Integer.parseInt(st.nextToken());
arr[x]=y;
}
bfs(1);
br.close();
}
private static void bfs(int start){
Queue<Integer> q=new LinkedList<>();
int[] visit=new int[101];
q.offer(start);
visit[start]=0;
while(!q.isEmpty()){
int cur=q.poll();
for(int i=1;i<=6;i++){
//다음 위치
int nw=cur+i;
if(nw<=0 || nw>100) continue;
if(visit[arr[nw]]==0){//방문한적 없을때
q.offer(arr[nw]);
visit[arr[nw]]=visit[cur]+1;
}
if(arr[nw]==100){
System.out.println(visit[100]);
return;
}
}
}
}
}