forked from wisdompeak/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
dd9c398
commit abb01c3
Showing
1 changed file
with
13 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
### 1036.Escape-a-Large-Maze | ||
|
||
首先,对于1M by 1M的网格内,想要进行任意两点间的寻路几乎是不可能的。本题的关键在于blocked的位置最多只有200个。 | ||
|
||
正常情况下,200个元素可以围成最大的区域面积是多少呢?肯定是一个中心对称的图形。我们如果从起点走一步,可以发现能走到的范围是一个边长是2、倾斜了45度的正方形。走两步的话,能覆盖的范围是一个边长是3、倾斜了45度的正方形。依次类推,考虑总周长为200的正方形,说明其边长是50,也就是从起点最多走49步。这种正方形最紧凑,是固定周长的条件下能够覆盖的最大的面积(2500)。 | ||
|
||
于是我们换个角度想:如果从起点出发向外扩散,并且最终可以扩展到超过2500个元素的区域(且没有遇到终点),这说明什么?因为根本没有周长是200的图形可以封闭住这么的面积,所以blocked对于这个起点而言是无效的。也就是说,blocked并不能完全围住起点。 | ||
|
||
同理,如果我们又发现blocked也不能围住终点的话,那么说明起点和终点势必会相遇。 | ||
|
||
所以这是一个BFS题,只要从一个点出发开始发散,当visited的网格数目(也就是覆盖的面积)大于2500的时候,就说明这个点并没有被封闭。 | ||
|
||
有了这个基本思路后,我们需要注意,其实200的周长最大能封闭的面积可以是19900,而不是2500.原因是这200个点可以以45度倾斜地围住一个角。因此```0+1+2+...+199 = 19900```才是最大的封闭面积。只有发散的区域超过了这个面积,才能保证不被封闭。 |