Escape The Ghosts - Problem
Escape The Ghosts - A PAC-MAN Style Challenge
Imagine you're playing a simplified version of PAC-MAN on an infinite 2D grid! You start at the origin
The Rules:
• You and all ghosts move simultaneously each turn
• Each turn, anyone can move 1 unit in any cardinal direction (north, east, south, west) or stay still
• If you reach the same square as a ghost at the same time (including the target), you lose
• You win only if you reach the target before any ghost reaches you
Your Mission: Determine if it's possible to escape regardless of how the ghosts move - meaning you have a guaranteed winning strategy!
Imagine you're playing a simplified version of PAC-MAN on an infinite 2D grid! You start at the origin
[0, 0] and need to reach a target destination [x_target, y_target] before any ghosts catch you.The Rules:
• You and all ghosts move simultaneously each turn
• Each turn, anyone can move 1 unit in any cardinal direction (north, east, south, west) or stay still
• If you reach the same square as a ghost at the same time (including the target), you lose
• You win only if you reach the target before any ghost reaches you
Your Mission: Determine if it's possible to escape regardless of how the ghosts move - meaning you have a guaranteed winning strategy!
Input & Output
example_1.py — Basic Escape
$
Input:
ghosts = [[1, 0], [0, 3]], target = [0, 1]
›
Output:
true
💡 Note:
Player needs 1 step to reach [0,1]. Ghost 1 needs |0-1| + |1-0| = 2 steps. Ghost 2 needs |0-0| + |1-3| = 2 steps. Since both ghosts need more steps than the player, escape is possible.
example_2.py — Ghost Intercepts
$
Input:
ghosts = [[1, 0]], target = [2, 0]
›
Output:
false
💡 Note:
Player needs 2 steps to reach [2,0]. The ghost at [1,0] needs |2-1| + |0-0| = 1 step. Since the ghost can reach the target faster, it can intercept the player there.
example_3.py — Equal Distance Edge Case
$
Input:
ghosts = [[2, 0]], target = [1, 0]
›
Output:
false
💡 Note:
Both player and ghost need exactly 1 step to reach [1,0]. Since they arrive simultaneously, the player does not escape (ties count as losses).
Visualization
Tap to expand
Understanding the Visualization
1
Calculate Your Distance
You start at the pizza shop (origin) and calculate straight-line city blocks to the customer
2
Check Competitor Distances
Each competitor calculates their city-block distance to the same customer
3
Compare Routes
If any competitor is closer or equally close, they'll arrive first or tie
4
Determine Winner
You win only if you're strictly closer than all competitors
Key Takeaway
🎯 Key Insight: In games where everyone moves at the same speed, Manhattan distance determines the winner - no complex simulation needed!
Time & Space Complexity
Time Complexity
O(n)
Single pass through all ghosts to calculate distances
✓ Linear Growth
Space Complexity
O(1)
Only storing constant number of variables
✓ Linear Space
Constraints
- 1 ≤ ghosts.length ≤ 100
- -104 ≤ ghosts[i][0], ghosts[i][1] ≤ 104
- -104 ≤ target[0], target[1] ≤ 104
- All points are distinct
- Key constraint: Everyone moves at the same speed (1 unit per turn)
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code