[Leetcode 1036] Escape a Large Maze | XingXing Park



[Leetcode 1036] Escape a Large Maze | XingXing Park

In a 1 million by 1 million grid, the coordinates of each grid square are (x, y) with 0 <= x, y < 10^6.

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.

Note:

  1. 0 <= blocked.length <= 200
  2. blocked[i].length == 2
  3. 0 <= blocked[i][j] < 10^6
  4. source.length == target.length == 2
  5. 0 <= source[i][j], target[i][j] < 10^6
  6. source != target

解题思路

题目给我们一个1百万乘1百万的迷宫,同时告诉你其中一些点是阻隔的,不能通过。问你是否有一条通路可以从点source连接到target

按照常规思路,我们首先想到的肯定是用DFS或者BFS的方法,从一个点开始,暴力搜索所有能到的点。但是,由于迷宫非常大,这样做的时间复杂度会达到O(10^12),你提交的解法会 TLE。

我们回头仔细看题目给我们的Note, 发现blocked的长度是有限制的,最多不超过200。这能给我们带来启发。首先,两个点之所以不能联通,一定是因为被blocked中的点分隔开了。那意味着blocked中的点能将迷宫分割成两个部分,每个部分分别包含sourcetarget中的一个点。而因为blocked的点数量不超过200,因此,它所能围城的面积也是有限制的。

我们可以将问题抽象成这样一个数学问题,在一个1000000 \* 1000000的矩形中,用一条长200的线,最多能围出多大的面积?这个问题可以用泛函的知识求解,这里不多做说明。但其实我们利用对称性可以知道,在任意一个角,围出一个弧长2001/4圆就是最大的面积,也就是4/pi \* 10000

知道了这个面积,我们只需要对sourcetarget分别做两次BFS。每次BFS,我们设定所搜的次数不超过我们求出的这个最大面积。如果在这些点中找到了targetsource,那自然说明有这样一条通路。否则:

  1. 如果我们发现BFS在我们设定的搜索次数内,已经完成,那么说明source或者target处于被blocked点和迷宫边界构成的一个封闭区间内,不存在通路。
  2. 如果BFS在设定的搜索次数内没有完成,说明并没有这样一个封闭区间能包裹住source或者target,那它们两个点一定是能够连通的。


Read full article from [Leetcode 1036] Escape a Large Maze | XingXing Park


No comments:

Post a Comment

Labels

Algorithm (219) Lucene (130) LeetCode (97) Database (36) Data Structure (33) text mining (28) Solr (27) java (27) Mathematical Algorithm (26) Difficult Algorithm (25) Logic Thinking (23) Puzzles (23) Bit Algorithms (22) Math (21) List (20) Dynamic Programming (19) Linux (19) Tree (18) Machine Learning (15) EPI (11) Queue (11) Smart Algorithm (11) Operating System (9) Java Basic (8) Recursive Algorithm (8) Stack (8) Eclipse (7) Scala (7) Tika (7) J2EE (6) Monitoring (6) Trie (6) Concurrency (5) Geometry Algorithm (5) Greedy Algorithm (5) Mahout (5) MySQL (5) xpost (5) C (4) Interview (4) Vi (4) regular expression (4) to-do (4) C++ (3) Chrome (3) Divide and Conquer (3) Graph Algorithm (3) Permutation (3) Powershell (3) Random (3) Segment Tree (3) UIMA (3) Union-Find (3) Video (3) Virtualization (3) Windows (3) XML (3) Advanced Data Structure (2) Android (2) Bash (2) Classic Algorithm (2) Debugging (2) Design Pattern (2) Google (2) Hadoop (2) Java Collections (2) Markov Chains (2) Probabilities (2) Shell (2) Site (2) Web Development (2) Workplace (2) angularjs (2) .Net (1) Amazon Interview (1) Android Studio (1) Array (1) Boilerpipe (1) Book Notes (1) ChromeOS (1) Chromebook (1) Codility (1) Desgin (1) Design (1) Divide and Conqure (1) GAE (1) Google Interview (1) Great Stuff (1) Hash (1) High Tech Companies (1) Improving (1) LifeTips (1) Maven (1) Network (1) Performance (1) Programming (1) Resources (1) Sampling (1) Sed (1) Smart Thinking (1) Sort (1) Spark (1) Stanford NLP (1) System Design (1) Trove (1) VIP (1) tools (1)

Popular Posts