forked from AnasImloul/Leetcode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSnakes and Ladders.java
73 lines (68 loc) · 2.73 KB
/
Snakes and Ladders.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
class Solution {
public int snakesAndLadders(int[][] board) {
if(board == null || board.length == 0) return 0; //Empty Input
int n = board.length;
HashMap<Integer,Integer> map = new HashMap<>();
getSnakesAndLadders(board , map); //this function will get snake&ladder positions in grid
int target = n*n;
boolean[] visited = new boolean[target + 1]; //assumeing 1based index so +1;
ArrayDeque<Integer> queue = new ArrayDeque<>();
queue.add(1);
visited[1] = true;
int steps = 0;
while(queue.isEmpty() == false){
int size = queue.size();
for(int i = 0; i < size; i++){
int current = queue.poll();
if(current == target){
return steps;
}
//go for all possiblities
for(int dice = 1; dice <= 6; dice++){
int jump = current + dice;
if(jump > target){
break;
}
//if this ladder or snake already used then don't use again
if(map.containsKey(jump) && visited[map.get(jump)]) continue;
//if there is ladder on place which not used already
if(map.containsKey(jump) && visited[map.get(jump)] == false){
visited[jump] = true; //start of ladder
queue.add(map.get(jump));
visited[map.get(jump)] = true; //end of ladder
}else if(visited[jump] == false){
queue.add(jump);
visited[jump] = true;
}
}
}
steps += 1;
}
return -1;
}
//function to traverse spiraly and get all snake&ladder positions
public void getSnakesAndLadders(int[][] board , HashMap<Integer,Integer> map){
int startRow = 0;
int position = 1;
for(int row = board.length-1; row >= 0; row--){
if(startRow%2 == 0){
for(int col = 0; col < board.length; col++){
if(board[row][col] != -1){
int dest = board[row][col];
map.put(position , dest);
}
position += 1;
}
}else{
for(int col = board.length-1; col >= 0; col--){
if(board[row][col] != -1){
int dest = board[row][col];
map.put(position, dest);
}
position += 1;
}
}
startRow += 1;
}
}
}