[Swift]LeetCode789. 逃脱阻碍者 | Escape The Ghosts

[Swift]LeetCode789. 逃脱阻碍者 | Escape The Ghosts,第1张

概述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], gh

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

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.

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.

Example 1:input: ghosts = [[1,0],[0,3]]target = [0,1]Output: trueExplanation: 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: falseExplanation: 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: falseExplanation: The ghost can reach the target at the same time as you.

Note:

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

你在进行一个简化版的吃豆人游戏。你从 (0,0)@H_419_8@ 点开始出发,你的目的地是 (target[0],target[1])@H_419_8@ 。地图上有一些阻碍者,第 i 个阻碍者从 (ghosts[i][0],ghosts[i][1])@H_419_8@ 出发。

每一回合,你和阻碍者们*可以*同时向东,西,南,北四个方向移动,每次可以移动到距离原位置1个单位的新位置。

如果你可以在任何阻碍者抓住你之前到达目的地(阻碍者可以采取任意行动方式),则被视为逃脱成功。如果你和阻碍者同时到达了一个位置(包括目的地)都不算是逃脱成功。

当且仅当你有可能成功逃脱时,输出 True。

示例 1:输入: ghosts = [[1,1]输出:true解释:你可以直接一步到达目的地(0,1),在(1,0)或者(0,3)位置的阻碍者都不可能抓住你。 
示例 2:输入: ghosts = [[1,0]输出:false解释:你需要走到位于(2,0)的目的地,但是在(1,0)的阻碍者位于你和目的地之间。 
示例 3:输入: ghosts = [[2,0]输出:false解释:阻碍者可以和你同时达到目的地。 

说明:

所有的点的坐标值的绝对值 <= 10000@H_419_8@。 阻碍者的数量不会超过 100@H_419_8@。 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 false10             }11         }12         return true13     }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.max6         7         return path < minGhostPath8     }9 }
总结

以上是内存溢出为你收集整理的[Swift]LeetCode789. 逃脱阻碍者 | Escape The Ghosts全部内容,希望文章能够帮你解决[Swift]LeetCode789. 逃脱阻碍者 | Escape The Ghosts所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1018734.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-23
下一篇 2022-05-23

发表评论

登录后才能评论

评论列表(0条)