Skip to content

Commit 114a6d2

Browse files
committed
Merge branch 'add-snakes_and_ladders_min_turns_solution'
2 parents b961df7 + c46e7d2 commit 114a6d2

File tree

4 files changed

+162
-0
lines changed

4 files changed

+162
-0
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
This problem was asked by **Flipkart**.
2+
3+
[Snakes and Ladders](https://en.wikipedia.org/wiki/Snakes_and_ladders) is a game played on a `10 x 10` board, the goal of which is get from square `1` to square `100`. On each turn players will roll a six-sided die and move forward a number of spaces equal to the result. If they land on a square that represents a snake or ladder, they will be transported ahead or behind, respectively, to a new square.
4+
5+
Find the smallest number of turns it takes to play snakes and ladders.
6+
7+
For convenience, here are the squares representing snakes and ladders, and their outcomes:
8+
9+
```json
10+
snakes = {16: 6, 48: 26, 49: 11, 56: 53, 62: 19, 64: 60, 87: 24, 93: 73, 95: 75, 98: 78}
11+
ladders = {1: 38, 4: 14, 9: 31, 21: 42, 28: 84, 36: 44, 51: 67, 71: 91, 80: 100}
12+
```
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import java.util.HashMap;
2+
import java.util.Map;
3+
4+
public class Approach {
5+
6+
public static int minTurnSolution(Map<Integer, Integer> snakes, Map<Integer, Integer> ladders) {
7+
int[] dp = new int[100];
8+
9+
// Initialize dp array with high values (like Infinity)
10+
for (int i = 0; i < dp.length; i++) {
11+
dp[i] = Integer.MAX_VALUE;
12+
}
13+
14+
// Traverse through each square of the board
15+
for (int i = -1; i < 100; i++) {
16+
for (int j = i + 1; j <= i + 6 && j < 100; j++) {
17+
if (snakes.containsKey(j + 1)) {
18+
dp[j] = Integer.MAX_VALUE; // Skip the snake square
19+
} else if (j <= 6) {
20+
dp[j] = 1; // Early game, squares 1-6 can be reached in 1 turn
21+
} else {
22+
dp[j] = Math.min(dp[j], dp[i] == Integer.MAX_VALUE ? Integer.MAX_VALUE : dp[i] + 1);
23+
}
24+
25+
// Handle ladder jumps
26+
if (ladders.containsKey(j + 1)) {
27+
dp[ladders.get(j + 1) - 1] = Math.min(dp[ladders.get(j + 1) - 1], dp[j]);
28+
}
29+
}
30+
}
31+
32+
return dp[99];
33+
}
34+
35+
public static void main(String[] args) {
36+
// Define the snakes and ladders
37+
Map<Integer, Integer> snakes = new HashMap<>();
38+
snakes.put(16, 6);
39+
snakes.put(48, 26);
40+
snakes.put(49, 11);
41+
snakes.put(56, 53);
42+
snakes.put(62, 19);
43+
snakes.put(64, 60);
44+
snakes.put(87, 24);
45+
snakes.put(93, 73);
46+
snakes.put(95, 75);
47+
snakes.put(98, 78);
48+
49+
Map<Integer, Integer> ladders = new HashMap<>();
50+
ladders.put(1, 38);
51+
ladders.put(4, 14);
52+
ladders.put(9, 31);
53+
ladders.put(21, 42);
54+
ladders.put(28, 84);
55+
ladders.put(36, 44);
56+
ladders.put(51, 67);
57+
ladders.put(71, 91);
58+
ladders.put(80, 100);
59+
60+
// Call the function and print the result
61+
System.out.println(minTurnSolution(snakes, ladders)); // Output: 7
62+
}
63+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
function minTurnSolution(snakes, ladders) {
2+
let dp = Array(100).fill(Infinity);
3+
for (let i = -1; i < 100; i++) {
4+
for (let j = i + 1; j <= i + 6 && j < 100; j++) {
5+
if (j + 1 in snakes) dp[j] = Infinity;
6+
else if (j <= 6) dp[j] = 1;
7+
else {
8+
dp[j] = Math.min(dp[j], dp[i] + 1);
9+
}
10+
if (j + 1 in ladders)
11+
dp[ladders[j + 1] - 1] = Math.min(dp[ladders[j + 1] - 1], dp[j]);
12+
}
13+
}
14+
return dp[99];
15+
}
16+
17+
snakes = {
18+
16: 6,
19+
48: 26,
20+
49: 11,
21+
56: 53,
22+
62: 19,
23+
64: 60,
24+
87: 24,
25+
93: 73,
26+
95: 75,
27+
98: 78,
28+
};
29+
ladders = {
30+
1: 38,
31+
4: 14,
32+
9: 31,
33+
21: 42,
34+
28: 84,
35+
36: 44,
36+
51: 67,
37+
71: 91,
38+
80: 100,
39+
};
40+
41+
console.log(minTurnSolution(snakes, ladders));
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
def min_turn_solution(snakes, ladders):
2+
dp = [float('inf')] * 100
3+
4+
for i in range(-1, 100):
5+
for j in range(i + 1, min(i + 7, 100)):
6+
if (j + 1) in snakes:
7+
dp[j] = float('inf') # Skip the snake square
8+
elif j <= 6:
9+
dp[j] = 1 # Early game, first few squares can be reached in 1 turn
10+
else:
11+
dp[j] = min(dp[j], dp[i] + 1) # Normal dice roll progression
12+
13+
# Handle ladder jumps
14+
if (j + 1) in ladders:
15+
dp[ladders[j + 1] - 1] = min(dp[ladders[j + 1] - 1], dp[j])
16+
17+
return dp[99]
18+
19+
# Snakes and Ladders configuration
20+
snakes = {
21+
16: 6,
22+
48: 26,
23+
49: 11,
24+
56: 53,
25+
62: 19,
26+
64: 60,
27+
87: 24,
28+
93: 73,
29+
95: 75,
30+
98: 78,
31+
}
32+
33+
ladders = {
34+
1: 38,
35+
4: 14,
36+
9: 31,
37+
21: 42,
38+
28: 84,
39+
36: 44,
40+
51: 67,
41+
71: 91,
42+
80: 100,
43+
}
44+
45+
# Output the result
46+
print(min_turn_solution(snakes, ladders)) # Expected Output: 7

0 commit comments

Comments
 (0)