Skip to content

Commit a8f1d07

Browse files
author
Partho Biswas
committed
909. Snakes and Ladders
1 parent 7a021a3 commit a8f1d07

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -525,6 +525,7 @@ BFS, DFS, Dijkstra, Floyd–Warshall, Bellman-Ford, Kruskal, Prim's, Minimum Spa
525525
|29| **[947. Most Stones Removed with Same Row or Column](https://tinyurl.com/rrm6w2a)** | [Python](https://tinyurl.com/wu6rdaw/947_Most_Stones_Removed_with_Same_Row_or_Column.py), [Swift](https://tinyurl.com/wuja3c4/947_Most_Stones_Removed_with_Same_Row_or_Column.swift) | [Vid 1](https://tinyurl.com/sc2g6eg), [Art 1](https://tinyurl.com/sxyzxkr), [Vid 2](https://tinyurl.com/wocnrhn), [Art 2](https://tinyurl.com/sogsj74), [Art 3](https://tinyurl.com/w4pk463) | Medium |📌 DFS and Union FInd |
526526
|30| [1042. Flower Planting With No Adjacent](https://tinyurl.com/unwlzyy) | [Python](https://tinyurl.com/wu6rdaw/1042_Flower_Planting_With_No_Adjacent.py), [Swift](https://tinyurl.com/wuja3c4/1042_Flower_Planting_With_No_Adjacent.swift) | --- | Easy |📌 Could be done using DFS, BFS |
527527
|31| **[994_Rotting_Oranges](https://tinyurl.com/yavrnvu5)** | [Python](https://tinyurl.com/wu6rdaw/994_Rotting_Oranges.py), [Swift](https://tinyurl.com/wuja3c4/994_Rotting_Oranges.swift) | --- | Medium |📌 BFS |
528+
|32| **[909. Snakes and Ladders](https://tinyurl.com/yazkm38f)** | [Python](https://tinyurl.com/wu6rdaw/909_Snakes_and_Ladders.py), [Swift](https://tinyurl.com/wuja3c4/909_Snakes_and_Ladders.swift) | [Art 1](https://tinyurl.com/ya45spcc), [Art 2](https://tinyurl.com/ycafdfr4), [Art 3](https://tinyurl.com/yc3hdaw9), [Vid 1](https://tinyurl.com/yc3hdaw9), [Vid 2](https://tinyurl.com/y99hrfvk), | Medium |📌 BFS |
528529

529530

530531
</p>
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
"""
2+
[
3+
[-1,-1,-1,-1,-1,-1],
4+
[-1,-1,-1,-1,-1,-1],
5+
[-1,-1,-1,-1,-1,-1],
6+
[-1,35,-1,-1,13,-1],
7+
[-1,-1,-1,-1,-1,-1],
8+
[-1,15,-1,-1,-1,-1]]
9+
10+
[
11+
[4,-1],
12+
[-1,3]]
13+
14+
9, 6
15+
1, 3
16+
17+
18+
6-1-3
19+
"""
20+
21+
22+
class Solution(object):
23+
def snakesAndLadders(self, board):
24+
"""
25+
:type board: List[List[int]]
26+
:rtype: int
27+
"""
28+
rowCount = len(board)
29+
seen = set()
30+
queue = collections.deque()
31+
queue.append((1, 0)) # label, step
32+
while queue:
33+
label, step = queue.popleft()
34+
r, c = self.labelToPosition(label, rowCount)
35+
if board[r][c] != -1:
36+
label = board[r][c]
37+
if label == rowCount * rowCount: # we have reached to our destination
38+
return step
39+
for nextMove in range(1, 7):
40+
newLabel = label + nextMove
41+
if newLabel <= rowCount * rowCount and newLabel not in seen:
42+
seen.add(newLabel)
43+
queue.append((newLabel, step + 1))
44+
return -1
45+
46+
def labelToPosition(self, label, rowCount):
47+
r, c = divmod(label - 1, rowCount)
48+
if r % 2 == 0: # even number of row
49+
return rowCount - 1 - r, c
50+
else: # odd number of row
51+
return rowCount - 1 - r, rowCount - 1 - c

0 commit comments

Comments
 (0)