Skip to content

Commit 75746b1

Browse files
committed
added Escape Large Maze (hard)
1 parent b56e2d2 commit 75746b1

File tree

3 files changed

+72
-0
lines changed

3 files changed

+72
-0
lines changed

Hard/EscapeLargeMaze/README.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
2+
# Escape a Large Maze
3+
[Leetcode Link](https://leetcode.com/problems/escape-a-large-maze/)
4+
5+
## Problem:
6+
7+
In a 1 million by 1 million grid, the coordinates of each grid square are `(x, y)` with `0 <= x, y < 10^6`.
8+
9+
We start at the `source` square and want to reach the `target` square. Each move, we can walk to a 4-directionally adjacent square in the grid that isn't in the given list of `blocked` squares.
10+
11+
Return `true` if and only if it is possible to reach the target square through a sequence of moves.
12+
13+
## Example:
14+
15+
```
16+
Input: blocked = [[0,1],[1,0]], source = [0,0], target = [0,2]
17+
Output: false
18+
Explanation:
19+
The target square is inaccessible starting from the source square, because we can't walk outside the grid.
20+
```
21+
```
22+
Input: blocked = [], source = [0,0], target = [999999,999999]
23+
Output: true
24+
Explanation:
25+
Because there are no blocked cells, it's possible to reach the target square.
26+
```
27+
28+
## Note:
29+
30+
- `0 <= blocked.length <= 200`
31+
- `blocked[i].length == 2`
32+
- `0 <= blocked[i][j] < 10^6`
33+
- `source.length == target.length == 2`
34+
- `0 <= source[i][j], target[i][j] < 10^6`
35+
- `source != target`

Hard/EscapeLargeMaze/solution.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
from typing import List
2+
3+
# test size is reduced to 10 x 10 grid for visual debugging
4+
class Solution:
5+
def isEscapePossible(self, blocked: List[List[int]], source: List[int], target: List[int]) -> bool:
6+
N = 10
7+
# queue for BFS
8+
queue = list()
9+
visited = set()
10+
queue.append(source)
11+
while queue:
12+
print("queue:", queue)
13+
print("visited:", visited)
14+
currentCoord = queue.pop(0)
15+
if currentCoord == target:
16+
return True
17+
visited.add(tuple(currentCoord))
18+
# get four neighboring coordinates and add to queue if not visited and is valid (not blocked and not outside boundary)
19+
if currentCoord[0]-1 >= 0 and (currentCoord[0]-1,currentCoord[1]) not in visited and [currentCoord[0]-1,currentCoord[1]] not in blocked:
20+
queue.append([currentCoord[0]-1, currentCoord[1]])
21+
if currentCoord[0]+1 < N and (currentCoord[0]+1, currentCoord[1]) not in visited and [currentCoord[0]+1, currentCoord[1]] not in blocked:
22+
queue.append([currentCoord[0]+1, currentCoord[1]])
23+
if currentCoord[1]-1 >= 0 and (currentCoord[0], currentCoord[1]-1) not in visited and [currentCoord[0], currentCoord[1]-1] not in blocked:
24+
queue.append([currentCoord[0], currentCoord[1]-1])
25+
if currentCoord[1]+1 < N and (currentCoord[0], currentCoord[1]+1) not in visited and [currentCoord[0], currentCoord[1]+1] not in blocked:
26+
queue.append([currentCoord[0], currentCoord[1]+1])
27+
28+
return False
29+
30+
31+
# test driver
32+
sol = Solution()
33+
blocked = [[1,2], [0,2], [2,2]]
34+
source = [0,0]
35+
target = [3,3]
36+
print("Output:", sol.isEscapePossible(blocked, source, target))

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ Languages used: Java and Python
8585
- [First Missing Positive](Hard/FirstMissingPositive)
8686
- [Insert Interval](Hard/InsertInterval)
8787
- [Find Median From Data Stream](Hard/FindMedianFromDataStream)
88+
- [Escape a Large Maze](Hard/EscapeLargeMaze)
8889

8990
---
9091

0 commit comments

Comments
 (0)