TomoLink
CompaniesAppleData Structures & AlgorithmsEscape a Large Maze
DSA
HardArray

Escape a Large Maze

arrayhash tabledepth-first search

Problem Statement

There is a 1 million by 1 million grid on an XY-plane, and the coordinates of each grid square are (x, y) .

We start at the source = [s _x , s _y ] square and want to reach the target = [t _x , t _y ] square. There is also an array of blocked squares, where each blocked[i] = [x _i , y _i ] represents a blocked square with coordinates (x _i , y _i ) .

Each move, we can walk one square north, east, south, or west if the square is not in the array of blocked squares. We are also not allowed to walk outside of the grid.

Return true if and only if it is possible to reach the target square from the source square through a sequence of valid moves .

Examples

Example 1
Input: blocked = [[0,1],[1,0]], source = [0,0], target = [0,2]
Output: false
The target square is inaccessible starting from the source square because we cannot move. We cannot move north or east because those squares are blocked. We cannot move south or west because we cannot go outside of the grid.
Example 2
Input: blocked = [], source = [0,0], target = [999999,999999]
Output: true
Because there are no blocked cells, it is possible to reach the target square.

Constraints

0 <= blocked.length <= 200
blocked[i].length == 2
0 <= x _i , y _i < 10 ^6
source.length == target.length == 2
0 <= s _x , s _y , t _x , t _y < 10 ^6
source != target
It is guaranteed that source and target are not blocked.
😤
Hard
Difficulty
Topic Info
ModuleDSA
CategoryArray
Sub-topicHash Table
Tags
arrayhash tabledepth-first searchbreadth-first search
Navigation
Escape a Large Maze [Hard] | Apple Dsa | TomoLink