-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTheQuickestWayUp.java
More file actions
118 lines (106 loc) · 2.53 KB
/
TheQuickestWayUp.java
File metadata and controls
118 lines (106 loc) · 2.53 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;
public class TheQuickestWayUp {
Graph g;
boolean[] visited;
int[] distance;
boolean[] snakesOrLadder;
class Graph{
int V;
List<Integer>[] adjL;
Graph(int V){
this.V = V;
adjL=new List[V+1];
}
void setAdj(int start,int end){
if(adjL[start]==null){
ArrayList<Integer> al=new ArrayList<Integer>();
al.add(end);
adjL[start]=al;
}
else{
adjL[start].add(end);
}
}
void initializeAdj(){
for(int i=1;i<=V;i++){
if(adjL[i]==null){
ArrayList<Integer> al=new ArrayList<Integer>();
for(int j=1;j<=6 && i+j<=V;j++){
al.add(i+j);
}
adjL[i]=al;
}
}
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int tc=sc.nextInt();
TheQuickestWayUp quickestWayUp = new TheQuickestWayUp();
for(int i=0;i<tc;i++){
quickestWayUp.g=quickestWayUp.new Graph(100);
quickestWayUp.intialize(1);
int laddres = sc.nextInt();
for(int j=0;j<laddres;j++){
int start = sc.nextInt();
int end = sc.nextInt();
quickestWayUp.g.setAdj(start, end);
quickestWayUp.snakesOrLadder[end]=true;
}
int snakes = sc.nextInt();
for(int j=0;j<snakes;j++){
int start = sc.nextInt();
int end = sc.nextInt();
quickestWayUp.g.setAdj(start, end);
quickestWayUp.snakesOrLadder[end]=true;
}
quickestWayUp.g.initializeAdj();
quickestWayUp.bfs();
System.out.println(quickestWayUp.distance[100]);
}
}
private void bfs() {
LinkedList<Integer> list = new LinkedList<Integer>();
list.add(1);
distance[1]=0;
visited[1]=true;
while(!list.isEmpty()){
Integer node =list.pollFirst();
ArrayList<Integer> al = (ArrayList)g.adjL[node];
boolean directjump = false;
if(al.size()==1 && snakesOrLadder[al.get(0)])
directjump=true;
Iterator<Integer> itr = al.iterator();
while(itr.hasNext()){
Integer next=itr.next();
if(next==100){
if(!directjump)
distance[next]=distance[node]+1;
else
distance[next]=distance[node];
return;
}
if(!visited[next]){
visited[next]=true;
list.add(next);
if(!directjump)
distance[next]=distance[node]+1;
else
distance[next]=distance[node]; }
}
}
}
private void intialize(int source) {
visited=new boolean[g.V+1];
distance=new int[g.V+1];
for(int i=1;i<=g.V;i++){
distance[i]=-1;
}
distance[source]=0;
snakesOrLadder=new boolean[g.V+1];
}
}