789. Escape The Ghosts Medium

@problem@discussion
#Array#Math



1/**
2 * [789] Escape The Ghosts
3 *
4 * You are playing a simplified PAC-MAN game on an infinite 2-D grid. You start at the point [0, 0], and you are given a destination point target = [xtarget, ytarget] that you are trying to get to. There are several ghosts on the map with their starting positions given as a 2D array ghosts, where ghosts[i] = [xi, yi] represents the starting position of the i^th ghost. All inputs are integral coordinates.
5 * Each turn, you and all the ghosts may independently choose to either move 1 unit in any of the four cardinal directions: north, east, south, or west, or stay still. All actions happen simultaneously.
6 * You escape if and only if you can reach the target before any ghost reaches you. If you reach any square (including the target) at the same time as a ghost, it does not count as an escape.
7 * Return true if it is possible to escape regardless of how the ghosts move, otherwise return false.
8 *  
9 * Example 1:
10 * 
11 * Input: ghosts = [[1,0],[0,3]], target = [0,1]
12 * Output: true
13 * Explanation: You can reach the destination (0, 1) after 1 turn, while the ghosts located at (1, 0) and (0, 3) cannot catch up with you.
14 * 
15 * Example 2:
16 * 
17 * Input: ghosts = [[1,0]], target = [2,0]
18 * Output: false
19 * Explanation: You need to reach the destination (2, 0), but the ghost at (1, 0) lies between you and the destination.
20 * 
21 * Example 3:
22 * 
23 * Input: ghosts = [[2,0]], target = [1,0]
24 * Output: false
25 * Explanation: The ghost can reach the target at the same time as you.
26 * 
27 *  
28 * Constraints:
29 * 
30 * 	1 <= ghosts.length <= 100
31 * 	ghosts[i].length == 2
32 * 	-10^4 <= xi, yi <= 10^4
33 * 	There can be multiple ghosts in the same location.
34 * 	target.length == 2
35 * 	-10^4 <= xtarget, ytarget <= 10^4
36 * 
37 */
38pub struct Solution {}
39
40// problem: https://leetcode.com/problems/escape-the-ghosts/
41// discuss: https://leetcode.com/problems/escape-the-ghosts/discuss/?currentPage=1&orderBy=most_votes&query=
42
43// submission codes start here
44
45impl Solution {
46    pub fn escape_ghosts(ghosts: Vec<Vec<i32>>, target: Vec<i32>) -> bool {
47        false
48    }
49}
50
51// submission codes end
52
53#[cfg(test)]
54mod tests {
55    use super::*;
56
57    #[test]
58    fn test_789() {
59    }
60}
61


Back
© 2025 bowen.ge All Rights Reserved.