Swift   发布时间:2022-03-31  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了[Swift]LeetCode789. 逃脱阻碍者 | Escape The Ghosts大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

概述

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),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]).

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

  • 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]) 出发。

每一回合,你和阻碍者们*可以*同时向东,西,南,北四个方向移动,每次可以移动到距离原位置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
  • 阻碍者的数量不会超过 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 }

大佬总结

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

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

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。