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:
1
💡 Note:
Start at (0,0) → move right to (0,1) which has portal 'A' → teleport to (2,2) which is the destination. Total: 1 move (teleportation doesn't count as a move).
example_2.py — Multiple Portals
$
Input:
[
"A.B",
"...",
"B.A"
]
›
Output:
0
💡 Note:
Start at (0,0) which has portal 'A' → teleport directly to (2,2) which is the destination. Total: 0 moves (teleportation doesn't count as a move).
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
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code