Grid Teleportation Traversal - Problem
Imagine you're a digital explorer navigating through a mystical 2D grid world filled with obstacles and magical teleportation portals! ๐
You start at the top-left corner (0, 0) and need to reach the bottom-right corner in the minimum number of moves. The grid contains:
'.'- Empty cells you can walk through'#'- Obstacles that block your path'A'-'Z'- Magical teleportation portals that can instantly transport you!
Here's the exciting part: when you step on a portal letter for the first time, you can instantly teleport to any other cell with the same letter anywhere in the grid! Each portal type can only be used once during your journey.
Goal: Find the minimum number of moves to reach the destination, or return -1 if it's impossible.
Input & Output
example_1.py โ Basic Grid with Portal
$
Input:
[
".A.",
"#.#",
"..A"
]
โบ
Output:
4
๐ก Note:
Start at (0,0) โ move right to (0,1) which has portal 'A' โ teleport to (2,2) โ move left to (2,1) โ move left to (2,0). Total: 4 moves (teleportation doesn't count as a move, but we need 4 regular moves).
example_2.py โ Multiple Portals
$
Input:
[
"A.B",
"...",
"B.A"
]
โบ
Output:
2
๐ก Note:
Start at (0,0) which has portal 'A' โ teleport to (2,2) โ move left to (2,1) โ move left to (2,0). Total: 2 moves.
example_3.py โ No Portal Needed
$
Input:
[
"...",
".#.",
"..."
]
โบ
Output:
4
๐ก Note:
Simple path without using any portals: (0,0) โ (0,1) โ (0,2) โ (1,2) โ (2,2). Total: 4 moves.
Constraints
- 1 โค m, n โค 100
- grid[i][j] is one of '.', '#', or an uppercase English letter
- The starting cell (0, 0) and ending cell (m-1, n-1) are guaranteed to not be obstacles ('#')
- Each portal letter can be used at most once during the journey
- Teleportation does not count as a move
Visualization
Tap to expand
Understanding the Visualization
1
Map Exploration
Start at the entrance and identify all available portals and their destinations
2
State Tracking
Keep track of your current position AND which portals you've already used
3
BFS Search
Explore all possible moves level by level, ensuring shortest path
4
Portal Decision
At each portal, decide whether using it now leads to a better path
Key Takeaway
๐ฏ Key Insight: Use BFS with state tracking (position + used portals) to guarantee the shortest path while respecting portal usage constraints. Each portal creates a branching point in our search space!
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code