789.逃脱阻碍者
链接:789.逃脱阻碍者
难度:Medium
标签:数组、数学
简介:你在进行一个简化版的吃豆人游戏。 只有在你有可能成功逃脱时,输出 true ;否则,输出 false 。
题解 1 - typescript
- 编辑时间:2021-08-22
- 执行用时:84ms
- 内存消耗:39.4MB
- 编程语言:typescript
- 解法介绍:曼哈顿距离,只有阻碍者比起始点远才可到达。
function escapeGhosts(ghosts: number[][], target: number[]): boolean {
const comp = (x: number, y: number): number => Math.abs(x - target[0]) + Math.abs(y - target[1]);
const distance = comp(0, 0);
for (const ghost of ghosts) {
if (comp(...(ghost as [number, number])) <= distance) return false;
}
return true;
}