-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSnakeAndLadder.java
More file actions
55 lines (50 loc) · 1.42 KB
/
SnakeAndLadder.java
File metadata and controls
55 lines (50 loc) · 1.42 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
import java.util.*;
public class SnakeAndLadder {
static Map<Integer, Integer> mp;
static class QueueEntry{
int v, dist;
QueueEntry(int v, int dist) {
this.v = v;
this.dist = dist;
}
public String toString() {
return "[" + this.v + ", " + this.dist + "]";
}
}
static int throwsCnt() {
int visited[] = new int[30];
Queue<QueueEntry> q = new LinkedList<>();
QueueEntry qe = new QueueEntry(0, 0);
visited[0] = 1;
q.add(qe);
int n = 30;
while(!q.isEmpty()) {
qe = q.poll();
int v = qe.v;
if(v == (n-1)) break;
for(int j=(v+1); j<=(v+6) && j<n;j++) {
if(visited[j] == 0) {
QueueEntry a = new QueueEntry(j, qe.dist+1);
visited[j] = 1;
if(mp.containsKey(j)) a.v = mp.get(j);
q.add(a);
}
}
System.out.println(q);
}
return qe.dist;
}
static int minThrow(int N, int a[]){
mp = new HashMap<>();
for(int i=0;i<2*N;i++) {
if(i%2==0) mp.put(a[i]-1, a[i+1]-1);
}
//System.out.println(mp);
return throwsCnt();
}
public static void main(String args[]) {
int a[] = {22,9};
int N = 1;
System.out.println(minThrow(N,a));
}
}