Open In App

Tutorial on Path Problems in a Grid, Maze, or Matrix

Last Updated : 28 Nov, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Path in a Grid or Maze refers to problems that involve navigating through a grid-like structure from the source (starting point) to the destination (endpoint) while avoiding the obstacles i.e., following rules and constraints.

ratinmaze_filled_path1

Path in a Maze/Grid

Grid and Maze Representation:

A grid or a maze is generally represented as a 2D array or matrix consisting of rows and columns. Each cell is a intersection of a particular row and column and it represents a location in the grid. The cells in the grid may be open for traversal or may be blocked by an obstacle. If a cell is blocked by an obstacle that cell cannot be visited.

Common Pathfinding Problems:

  • Finding a Path: The most standard problem involves finding path from a given starting point to a given ending point in the grid. This often involves finding a sequence of connected cells that lead from the start to the end.
    Example:- Given a N*N grid, each cell of a grid is either a ‘.’ representing a empty space or a ‘#’ representing a obstacle. The task is to determine any valid path from source (1,1) to destination (N,N) without visiting any obstacle.
  • Counting Paths: In some of the problems we have find the total number of unique paths from the starting point to the ending point under some rules and constraints.
    Example:- Given a N*N grid, the task is to find all possible paths that the rat can take to reach from source to destination. The directions in which the you can move are ‘U'(up), ‘D'(down), ‘L’ (left), ‘R’ (right). Value 0 at a cell in the matrix represents that it is blocked and one cannot move to it while value 1 at a cell in the matrix represents that one can be travel through it.
  • Shortest Path: In some of the problems, the task will be to find the shortest path from the source to the destination. The shortest path is the minimum number of steps to reach destination.
    Example:-In this problem a MxN matrix is given where each cell can either be 0 or 1. We need to find the shortest path between a given source cell to a destination cell. The path can only be created out of a cell if its value is 1.

Common Algorithms in Pathfinding Problems:

Pathfinding problems often require the use of various algorithms like:-

  • Depth-First Search (DFS): DFS is generally used to traverse the maze and to explore various paths. This algorithm is basically used in all of the grid/maze problems because in every problem we have to traverse the grid.
  • Backtracking: Backtracking in Maze Problems is used when exploring all possible paths from source to destination is needed.
    Example:- A rat is placed at (0, 0) in a square matrix m[ ][ ] of order n and has to reach the destination at (n-1, n-1). The task is to find a sorted array of strings denoting all the possible directions which the rat can take to reach the destination at (n-1, n-1). The directions in which the rat can move are ‘U'(up), ‘D'(down), ‘L’ (left), ‘R’ (right).
  • Dynamic Programming: Dynamic Programming is used when we need to avoid redundant calculations in Maze Problems. It is generally used when we have to find the number of paths from source to destination under some constraints.
    Example:- Given a grid with each cell consisting of positive, negative or no points i.e, zero points. We can move across a cell only if we have positive points ( > 0 ). Whenever we pass through a cell, points in that cell are added to our overall points. We need to find minimum initial points to reach cell (m-1, n-1) from (0, 0).
  • Breadth-First Search (BFS): BFS is used when we need to find the shortest path from source to destination.
    Example:- Given a n*m grid. You are at point (0, 0) and must reach point (n-1, m-1). If any cell contains 0, it is a free cell, if it contains 1, there is an obstacle and you can’t pass through the cell, and if it contains 2, that means it is free and if you step into this cell, you can pass through any adjacent cell of it that contains obstacles. In one step you can go one unit in any four directions (Inside the grid), the task is to find the minimum number of steps to reach the destination if it is impossible return -1.

Given below are the most common Grid/Maze problems:

Problems

Links

Rat in a Maze

Link

Rat in a Maze Problem when movement in all possible directions is allowed

Link

Rat in a Maze with multiple steps or jump allowed

Link

Unique paths in a Grid with Obstacles

Link

Count number of ways to reach destination in a Maze

Link

Minimum distance to the end of a grid from source

Link

Shortest path in a Binary Maze

Link

Find paths from corner cell to middle cell in maze

Link

Geek in a Maze

Link

Minimum moves required to come out of a grid safely

Link

Minimum Initial Points to Reach Destination

Link

Moving on grid

Link

Minimum cost to cover the given positions in a N*M grid

Link

Collect maximum points in a grid using two traversals

Link

Number of ways to go from one point to another in a grid

Link

Maximum Score by travelling from (1, 1) to (N, N) on grid

Link



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads