You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In a 1 million by 1 million grid, the coordinates of each grid square are (x, y).
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.
Return true if and only if it is possible to reach the target square through a sequence of moves.
Example 1:
Input: blocked = [[0,1],[1,0]], source = [0,0], target = [0,2]
Output: false
Explanation: The target square is inaccessible starting from the source square, because we can't walk outside the grid.
Example 2:
Input: blocked = [], source = [0,0], target = [999999,999999]
Output: true
Explanation: Because there are no blocked cells, it's possible to reach the target square.
Constraints:
0 <= blocked.length <= 200
blocked[i].length == 2
0 <= blocked[i][j] < 10^6
source.length == target.length == 2
0 <= source[i][j], target[i][j] < 10^6
source != target
这道题说是有一个一百万乘以一百万大小的格子,有个起始坐标 source 和一个终点坐标 target,还给了一个黑名单 blocked,说是上面的点都不能经过,现在问能否从起点到达终点。首先这道题是一道 Hard 题目,要给予充分的尊重,如果你以为这是简单的迷宫遍历问题的话,那就大错特错了,为啥呢,注意看一下格子的大小,一百万乘以一百万,分分钟让你记录遍历过位置的集合爆栈,这里由于迷宫过于庞大,不一定能直接遍历到终点。其实这道题的解法挺难想的,要从黑名单的长度下手,题目中限定了黑名单的大小不超过 200,那么来思考用 200 个点能多能封闭多大的空间,如下所示:
0th _________________________
|O O O O O O O X
|O O O O O O X
|O O O O O X
|O O O O X
.O O O X
.O O X
.O X
200th |X
Activity