[Swift]LeetCode789. 逃脫阻礙者 | Escape The Ghosts

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

You are playing a simplified Pacman game. You start at the point (0, 0), and your destination is (target[0], target[1]). There are several ghosts on the map, the i-th ghost starts at (ghosts[i][0], ghosts[i][1]).git

Each turn, you and all ghosts simultaneously *may* move in one of 4 cardinal directions: north, east, west, or south, going from the previous point to a new point 1 unit of distance away.github

You escape if and only if you can reach the target before any ghost reaches you (for any given moves the ghosts may take.)  If you reach any square (including the target) at the same time as a ghost, it doesn't count as an escape.微信

Return True if and only if it is possible to escape.spa

Example 1:
Input: 
ghosts = [[1, 0], [0, 3]]
target = [0, 1]
Output: true
Explanation: 
You can directly reach the destination (0, 1) at time 1, while the ghosts located at (1, 0) or (0, 3) have no way to catch up with you.
Example 2:
Input: 
ghosts = [[1, 0]]
target = [2, 0]
Output: false
Explanation: 
You need to reach the destination (2, 0), but the ghost at (1, 0) lies between you and the destination.
Example 3:
Input: 
ghosts = [[2, 0]]
target = [1, 0]
Output: false
Explanation: 
The ghost can reach the target at the same time as you.

Note:code

  • All points have coordinates with absolute value <= 10000.
  • The number of ghosts will not exceed 100.

你在進行一個簡化版的吃豆人遊戲。你從 (0, 0) 點開始出發,你的目的地是 (target[0], target[1]) 。地圖上有一些阻礙者,第 i 個阻礙者從 (ghosts[i][0], ghosts[i][1]) 出發。htm

每一回合,你和阻礙者們*能夠*同時向東,西,南,北四個方向移動,每次能夠移動到距離原位置1個單位的新位置。blog

若是你能夠在任何阻礙者抓住你以前到達目的地(阻礙者能夠採起任意行動方式),則被視爲逃脫成功。若是你和阻礙者同時到達了一個位置(包括目的地)都不算是逃脫成功。遊戲

當且僅當你有可能成功逃脫時,輸出 True。get

示例 1:
輸入: 
ghosts = [[1, 0], [0, 3]]
target = [0, 1]
輸出:true
解釋:
你能夠直接一步到達目的地(0,1),在(1, 0)或者(0, 3)位置的阻礙者都不可能抓住你。 
示例 2:
輸入: 
ghosts = [[1, 0]]
target = [2, 0]
輸出:false
解釋:
你須要走到位於(2, 0)的目的地,可是在(1, 0)的阻礙者位於你和目的地之間。 
示例 3:
輸入: 
ghosts = [[2, 0]]
target = [1, 0]
輸出:false
解釋:
阻礙者能夠和你同時達到目的地。 

說明:

  • 全部的點的座標值的絕對值 <= 10000
  • 阻礙者的數量不會超過 100

Runtime: 28 ms
Memory Usage: 18.8 MB
 1 class Solution {
 2     func escapeGhosts(_ ghosts: [[Int]], _ target: [Int]) -> Bool {
 3         var dist:Int = abs(target[0]) + abs(target[1])
 4         for ghost in ghosts
 5         {
 6             var t:Int = abs(ghost[0] - target[0]) + abs(ghost[1] - target[1])
 7             if t <= dist
 8             {
 9                 return false
10             }
11         }
12         return true
13     }
14 }

36ms

1 class Solution {
2     func escapeGhosts(_ ghosts: [[Int]], _ target: [Int]) -> Bool {
3         return distance([0, 0], target) < ghosts.map {distance($0, target)}.min()!
4     }
5 
6     func distance(_ point: [Int], _ target: [Int]) -> Int {
7         return abs(point[0] - target[0]) + abs(point[1] - target[1])
8     }
9 }

44ms

1 class Solution {
2     func escapeGhosts(_ ghosts: [[Int]], _ target: [Int]) -> Bool {
3         let path = abs(target[0]) + abs(target[1])
4         
5         let minGhostPath = ghosts.map({ abs(target[0] - $0[0]) + abs(target[1] - $0[1]) }).min() ?? Int.max
6         
7         return path < minGhostPath
8     }
9 }
相關文章
相關標籤/搜索