Skip to content

Commit 6e0eee4

Browse files
author
Manjunath
committed
snakes and ladder
1 parent b1f8393 commit 6e0eee4

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
'use strict';
2+
3+
function minimumMoves(visited, moves, n) {
4+
let result = {};
5+
result.vertex = 0;
6+
result.distance = 0;
7+
let queue = [result];
8+
let temp = null;
9+
while (queue.length != 0) {
10+
temp = queue.shift();
11+
if (result.vertex == n) break;
12+
for (let index = (temp.vertex + 1); index <= (temp.vertex + 6) && (temp.vertex < n); index++) {
13+
if (visited[index] == 0) {
14+
let qEntry = {};
15+
qEntry.distance = temp.distance + 1;
16+
visited[index] = 1;
17+
if (moves[index] != -1) {
18+
qEntry.vertex = moves[index];
19+
} else qEntry.vertex = index;
20+
queue.push(qEntry);
21+
}
22+
}
23+
}
24+
return temp.distance;
25+
}
26+
27+
let visited = [];
28+
let moves = [];
29+
let n = 100;
30+
for (let index = 0; index < n; index++) {
31+
visited[index] = 0;
32+
moves[index] = -1;
33+
}
34+
// Ladders Positions
35+
moves[2] = 23;
36+
moves[19] = 32;
37+
moves[66] = 82;
38+
moves[17] = 53;
39+
40+
// Snakes Positions
41+
moves[99] = 65;
42+
moves[36] = 18;
43+
moves[74] = 47;
44+
console.log('Minimum number of moves ', minimumMoves(visited, moves, n))

0 commit comments

Comments
 (0)