解题思路:
题目看似复杂,其实本质并不是让你遍历所有的路径,观察是否有没被抓住的情况,而是只用考虑极端情况,即计算移动者到达终点的最短距离以及阻挡者到达终点的最短距离,观察移动者的距离是否是最短的,如果不是最短的,那么一定会被抓住,返回false,否则返回true,代码如下:
class Solution {
public:
bool escapeGhosts(vector<vector<int>>& ghosts, vector<int>& target) {
// 定义最短距离
int num = abs(target[0]) + abs(target[1]);
int temp =INT_MAX;
for(auto& ghost : ghosts) {
// 计算别的阻断者的最短距离
temp = min(temp, (abs(ghost[0] - target[0]) + abs(ghost[1] - target[1])));
// 如果别的阻挡者可以更快到达终点
if(temp <= num) {
return false;
}
}
return true;
}
};