[Swift]LeetCode1036.逃離大迷宮 | Escape a Large Maze

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-xwnnobvf-mc.html 
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html

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

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.github

Return true if and only if it is possible to reach the target square through a sequence of moves.微信

Example 1:app

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:ide

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:spa

  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

在一個 10^6 x 10^6 的網格中,每一個網格塊的座標爲 (x, y),其中 0 <= x, y < 10^6code

咱們從源方格 source 開始出發,意圖趕往目標方格 target。每次移動,咱們均可以走到網格中在四個方向上相鄰的方格,只要該方格不在給出的封鎖列表 blocked 上。htm

只有在能夠經過一系列的移動到達目標方格時才返回 true。不然,返回 falseblog

示例 1:

輸入:blocked = [[0,1],[1,0]], source = [0,0], target = [0,2]
輸出:false
解釋:
從源方格沒法到達目標方格,由於咱們沒法在網格中移動。

示例 2:

輸入:blocked = [], source = [0,0], target = [999999,999999]
輸出:true
解釋:
由於沒有方格被封鎖,因此必定能夠到達目標方格。

提示:

  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

Runtime: 3096 ms
Memory Usage: 24.7 MB
 1 class Solution {
 2     func isEscapePossible(_ blocked: [[Int]], _ source: [Int], _ target: [Int]) -> Bool {
 3         var set:Set<Int> = Set<Int>()
 4         for b in blocked
 5         {
 6             set.insert(b[0]<<32|b[1])
 7             if set.contains(source[0]<<32|source[1]) {return false}
 8             if set.contains(target[0]<<32|target[1]) {return false}
 9         }
10         
11         var ss:[[Bool]] = go(source[0], source[1], set)
12         var ts:[[Bool]] = go(target[0], target[1], set)
13         if abs(source[0] - target[0]) > 200 || abs(source[1] - target[1]) > 200
14         {
15             var pt:Int  = 0
16             for i in 0..<411
17             {
18                 for j in 0..<411
19                 {
20                     if i == 0 || j == 0 || i == 410 || j == 410
21                     {
22                         if ss[i][j] {pt |= 1}
23                         if ts[i][j] {pt |= 2}
24                     }
25                 }
26             }
27             return pt == 3
28         }
29         else
30         {
31             return ss[target[0]-source[0]+205][target[1]-source[1]+205]
32         }
33     }
34     
35     func go(_ sr:Int,_ sc:Int,_ obs:Set<Int>) -> [[Bool]]
36     {
37         var can:[[Bool]] = [[Bool]](repeating: [Bool](repeating: false, count: 411), count: 411)
38         can[205][205] = true
39         var q:[[Int]] = [[Int]]()
40         q.append([205, 205])
41         let dr:[Int] = [ 1, 0, -1, 0 ]
42         let dc:[Int] = [ 0, 1, 0, -1 ]
43         while(!q.isEmpty)
44         {
45             var cur:[Int] = q.removeLast()
46             let r:Int = cur[0]
47             let c:Int = cur[1]
48             for k in 0..<4
49             {
50                 let nr:Int = r + dr[k]
51                 let nc:Int = c + dc[k]
52                 if nr >= 0 && nr < 411 && nc >= 0 && nc < 411 && !can[nr][nc]
53                     && sr-205+nr >= 0 && sr-205+nr < 1000000
54                     && sc-205+nc >= 0 && sc-205+nc < 1000000
55                     && !obs.contains((sr-205+nr)<<32|(sc-205+nc))
56                 {
57                     can[nr][nc] = true
58                     q.append([nr, nc])
59                 }
60             }
61         }
62         return can
63     }
64 }
相關文章
相關標籤/搜索