710283. Geek in a Maze

Hard

Here is a detailed Markdown blog post for the "Geek in a Maze" problem:

Geek in a Maze

Summary

The "Geek in a Maze" problem involves finding the shortest path from a starting point to an exit in a maze. The maze is represented as a graph, where each cell is connected to its neighbors (horizontally, vertically, and diagonally). The goal is to find the shortest path that allows the geek to escape the maze.

Detailed Explanation

The problem can be solved using Breadth-First Search (BFS) algorithm. BFS is an efficient algorithm for finding the shortest path in a graph or tree data structure.

Here's a step-by-step breakdown of the solution:

  1. Create a queue and enqueue the starting point.
  2. Mark the starting point as visited.
  3. While the queue is not empty, do the following:
    • Dequeue the next cell from the queue.
    • If this cell is the exit, return the path from the starting point to this cell (using backtracking).
    • For each unvisited neighbor of this cell, mark it as visited and enqueue it.
  4. If the queue becomes empty without finding the exit, there is no possible escape route.

Here's a diagram illustrating the BFS traversal:

  +-----+-----+
  |     |     |
  | S  ->| E    |
  |     |     |
  +-----+-----+
   ^         ^
   |         |
   |  visited  |
   |         |
   v         v
  +-----+-----+
  |     |     |
  | *  ->|*    |
  |     |     |
  +-----+-----+

In this diagram, S is the starting point, E is the exit, and * represents visited cells.

Time complexity: O(V + E), where V is the number of vertices (cells) in the maze and E is the number of edges (connections between cells). Space complexity: O(V), since we need to keep track of visited cells.

Optimized Solutions

Java

View on GeeksforGeeks
import java.util.*;

public class GeekInAMaze {
    public static int[] findShortestPath(int[][] maze, int startRow, int startCol, int exitRow, int exitCol) {
        Queue<int[]> queue = new LinkedList<>();
        boolean[][] visited = new boolean[maze.length][maze[0].length];
        queue.add(new int[]{startRow, startCol});
        visited[startRow][startCol] = true;

        while (!queue.isEmpty()) {
            int[] cell = queue.poll();
            if (cell[0] == exitRow && cell[1] == exitCol) {
                return backtrack(cell);
            }

            for (int[] neighbor : getNeighbors(maze, cell)) {
                if (!visited[neighbor[0]][neighbor[1]]) {
                    queue.add(neighbor);
                    visited[neighbor[0]][neighbor[1]] = true;
                }
            }
        }

        return null; // no escape route found
    }

    private static int[] backtrack(int[] cell) {
        List<int[]> path = new ArrayList<>();
        while (cell != null) {
            path.add(cell);
            cell = getPreviousCell(maze, cell);
        }
        return path.toArray(new int[0]);
    }

    private static int[][] getNeighbors(int[][] maze, int[] cell) {
        List<int[]> neighbors = new ArrayList<>();

        // check horizontal and vertical neighbors
        for (int i = -1; i <= 1; i++) {
            for (int j = -1; j <= 1; j++) {
                if (i == 0 && j == 0) continue;
                int newRow = cell[0] + i;
                int newCol = cell[1] + j;

                // check if neighbor is within maze boundaries and is not a wall
                if (newRow >= 0 && newRow < maze.length && newCol >= 0 && newCol < maze[0].length && maze[newRow][newCol] == 0) {
                    neighbors.add(new int[]{newRow, newCol});
                }
            }
        }

        return neighbors.toArray(new int[0][]);
    }

    private static int[] getPreviousCell(int[][] maze, int[] cell) {
        // implement this method to find the previous cell in the path
        // ...
    }
}

DevExCode

Master coding interviews with Leetcode, system design, TechBit, QuickLearn, DevTips, Tech Battles, and career services.

© 2026 DevExCode. All rights reserved.