Maze problem

Difficulty: MEDIUMCategory: LLDFrequency: 1Asked On 2024-09-03

Problem Statement

Designing a Maze system that allows representation, traversal, and pathfinding within a maze. The system should allow a user (or an agent) to navigate from a start point to an exit while considering walls, obstacles, and optionally multiple solutions.


Functional Requirements (FR)

Maze Representation


•Represent the maze as a grid or graph.
•Each cell may be walkable or blocked (wall/obstacle).
•Support different maze sizes (NxM).
•Optionally support start and end points.


Maze Operations


•Load or generate a maze.
•Print or visualize the maze.
•Mark cells as visited or unvisited.


Traversal & Pathfinding


•Find a path from start to end using algorithms like BFS, DFS, or A*.
•Support multiple pathfinding strategies (shortest path, all paths).
•Detect if a solution exists or not.


Movement


•Move an agent through the maze in four directions (up, down, left, right) or optionally eight directions (including diagonals).
•Track the agent’s current position.


Maze Solving Features


•Show solution path.
•Optionally mark visited paths for visualization.
•Support dynamic obstacles (walls added/removed at runtime).


Non-Functional Requirements (NFR)


•Efficiency: Pathfinding algorithms should be optimized for large mazes.
•Extensibility: Support different maze sizes, multiple start/end points.
•Visualization: Easy to represent maze state and traversal for debugging.
•Scalability: Handle large mazes efficiently (e.g., 1000x1000 grid).


Core Entities & Attributes


Maze


•rows, columns, grid[][] (2D array of Cell)
•startPoint, endPoint


Cell


•x, y, isWalkable, isVisited, parent (for path tracing)


Agent


•currentPosition, pathTaken


MazeSolver


•maze, algorithmType
•Methods: solve(), findAllPaths(), getShortestPath()


Path


•List of cells from start to end


APIs / Operations


•Maze.generate(rows, cols) → Generate a new maze.
•Maze.load(grid) → Load a predefined maze.
•Agent.move(direction) → Move agent if cell is walkable.
•MazeSolver.solve() → Solve the maze using a specific algorithm.
•MazeSolver.getPath() → Return solution path.
•Maze.printMaze() → Visual representation of maze.


Advanced Features (Optional / Bonus)


•Support multiple agents navigating simultaneously.
•Generate random mazes with guaranteed solvability.
•Dynamic real-time updates of maze obstacles.
•Visualize pathfinding step by step for educational purposes.
•Implement weighted mazes (different movement costs).


Solution

Comments