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
Grid Teleportation Traversal INPUT . S A . # . # . . A E Legend: . Empty # Block A Portal Start/End [".A.", "#.#", "..A"] 3x3 Grid Start:(0,0) Goal:(2,2) ALGORITHM (BFS) 1 Initialize BFS Queue: [(0,0,0)] dist=0 Map portals: A-->(0,1),(2,2) 2 Process Queue Pop cell, check neighbors Move: up/down/left/right 3 Use Teleport On portal, teleport to all same-letter cells 4 Track Distance BFS guarantees shortest path to destination Path Trace: 0,0 --> 0,1 --> 2,2 --> 2,1 move teleport! moves 1 + 1(teleport) + 2 = 4 moves Portal used once only! FINAL RESULT START d=0 A d=1 . # . # . . d=3,4 GOAL d=2 TELEPORT Output: 4 Minimum moves found! Path: (0,0)--(0,1)--A-- (2,2)--(2,1)--(2,2) [OK] BFS Complete Key Insight: BFS explores level by level, guaranteeing the shortest path. Teleportation portals act as instant edges connecting distant cells. By treating portal jumps as single moves and marking used portals, we avoid cycles and find optimal path. Here: 1 move to A, 1 teleport to goal-adjacent A, 2 more moves = 4 total. TutorialsPoint - Grid Teleportation Traversal | BFS Approach
Asked in
Google 45 Amazon 38 Meta 32 Microsoft 28
43.7K Views
High Frequency
~25 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen