LC 789. 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], 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.
Runtime: 4 ms, faster than 98.34% of C++ online submissions for Escape The Ghosts.
没有任何障碍,平面上两个点的最短距离就是坐标差。
class Solution {
public:
int dist(const vector<int>& a, const vector<int>& b){
return abs(a[] - b[]) + abs(a[] - b[]);
}
bool escapeGhosts(vector<vector<int>>& ghosts, vector<int>& target) {
int t = dist({,}, target);
for(int i=; i < ghosts.size(); i++){
if(dist(ghosts[i], target) <= t){
return false;
}
}
return true;
}
};
LC 789. Escape The Ghosts的更多相关文章
- [LeetCode] 789. Escape The Ghosts 逃离鬼魂
You are playing a simplified Pacman game. You start at the point (0, 0), and your destination is (ta ...
- LeetCode 789. Escape The Ghosts
题目链接:https://leetcode.com/problems/escape-the-ghosts/description/ You are playing a simplified Pacma ...
- 789. Escape The Ghosts
You are playing a simplified Pacman game. You start at the point (0, 0), and your destination is (ta ...
- 【LeetCode】789. Escape The Ghosts 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- [LeetCode] Escape The Ghosts 逃离鬼魂
You are playing a simplified Pacman game. You start at the point (0, 0), and your destination is (ta ...
- [Swift]LeetCode789. 逃脱阻碍者 | Escape The Ghosts
You are playing a simplified Pacman game. You start at the point (0, 0), and your destination is (ta ...
- 73th LeetCode Weekly Contest Escape The Ghosts
You are playing a simplified Pacman game. You start at the point (0, 0), and your destination is(tar ...
- LeetCode All in One题解汇总(持续更新中...)
突然很想刷刷题,LeetCode是一个不错的选择,忽略了输入输出,更好的突出了算法,省去了不少时间. dalao们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...
- leetcode 学习心得 (4)
645. Set Mismatch The set S originally contains numbers from 1 to n. But unfortunately, due to the d ...
随机推荐
- 记录-Intellij Idea下以Tomcat运行Web项目时的位置问题
今天本来准备把原来的一个Web项目导入到Idea下,之前这个项目是用eclipse写的,容器用的tomcat,首先导入前我把一些没用的配置文件都给删了,像什么.eclipse..setting什么的, ...
- 《python解释器源码剖析》第3章--python中的str对象
3.0 序 我们知道python中的字符串属于变长对象,当然和int也是一样,底层的结构体实例所维护的数据的长度,在对象没有定义的时候是不知道的.当然如果是python2的话,底层PyIntObjec ...
- (六)buildroot使用详解
为什么要使用buildroot? (文件系统搭建,强烈建议直接用buildroot,官网[http://buildroot.uclibc.org/]上有使用教程非常详细)文件系统通常要包含很多第三方软 ...
- Nginx中虚拟主机配置
一.Nginx中虚拟主机配置 1.基于域名的虚拟主机配置 1.修改宿主机的hosts文件(系统盘/windows/system32/driver/etc/HOSTS) linux : vim /etc ...
- JS 函数相关的声明调用
// 函数声明方法一 function f (a, b) { return a + b; } // 函数调用 console.log(f(1, 4)); // 函数声明方法二 var num = fu ...
- JVM的四种GC算法
程序在运行过程中,会产生大量的内存垃圾(一些没有引用指向的内存对象都属于内存垃圾,因为这些对象已经无法访问,程序用不了它们了,对程序而言它们已经死亡),为了确保程序运行时的性能,java虚拟机在程序运 ...
- 重温JSP学习笔记
<% double d1 = 3.5; double d2 = 4.4; pageContext.setAttribute("d1", d1); pageContext.se ...
- 十进制数转N进制c++实现
编写一个算法,将一个非负的十进制整数N转换为另一个基数为B的B进制整数. #include <iostream> #include<string.h> using namesp ...
- 超好用的富文本编辑器Summernote的使用
官网地址 中文文档 源码下载地址 Summernote依赖于jquery和bootstrap3/4 所以用时记得引入这俩依赖 奉上引入方法(官网说的很清楚,api也很详细): <!-- in ...
- 学习日记6、easyui datagrid 新增一行,编辑行,结束编辑和删除行操作记录
1.新增一行并进入编辑状态 var index=$('#Numbers').datagrid('appendRow', { CardInformation: '开户行', CardNumber: '银 ...