Grid problems involve a 2D grid of cells, often representing a map or graph. We can apply **Dynamic Programming**** **on Grids when the solution for a cell is dependent on solutions of previously traversed cells like to

**a path or**

**find****number of paths or solve an**

**count****across the grid, with certain constraints on movement or cost. In this article we are going to discuss about the idea behind**

**optimization problem****with their importance, use cases and some practice problems.**

**Dynamic Programming on Grids**Table of Content

__Idea behind Dynamic Programming (DP) on Grids:__

**1. Defining the state:**Â

**1. Defining the state:**Each cell in the grid represents a state,Â characterized by its position and any relevant information (e.g.,Â accumulated cost). Each cell is a state,Â uniquely identified by its coordinates (i,Â j). We can store additional information depending on the problem:

- ForÂ
:Â accumulated cost,Â direction of travel.**pathfinding** - ForÂ
:Â number of paths reaching the cell,Â specific conditions met.**counting paths** - ForÂ
:Â maximum/minimum value encountered so far.**maximum/minimum value**

__2. Defining the transition function:__

This function describes how to move from one state to another, specifying the valid moves and their associated costs. This function specifies how to move between states.

- ForÂ
:Â define valid moves (e.g.,Â up,Â down,Â left,Â right) and their associated cost (e.g.,Â distance,Â penalty).**pathfinding** - ForÂ
:Â enumerate all valid transitions based on problem conditions.**counting paths** - ForÂ
:Â compare current value with neighboring values and update accordingly.**maximum/minimum value**

__3. Defining the base cases:__

Define base cases to ensure that we do not move outside the grid. These are the smallest subproblems with readily known solutions:

- ForÂ
:Â The Starting and Ending cells often have a cost of 0.**pathfinding** - ForÂ
:Â The Starting cell/row/column is initialized to 1 as boundary of the grid might have only one path reaching them.**counting paths** - ForÂ
:Â Edges might have initial specific values.**maximum/minimum value**

__4. __**Iteratively filling the DP table:**Â

**Iteratively filling the DP table:**Starting from the base cases, calculate the optimal solution for each state using the transition function and the solutions to its smaller subproblems.

- Start with the base cases and fill the table cell by cell.
- For each cell (i,Â j):
- Use the
to consider all valid moves from neighboring cells.**transition function** - Calculate the
for the current cell based on the values of its neighbors and the transition function.**optimal value** - Update the
with the optimal value for cell (i,Â j).**DP table**

- Use the

__Importance of Dynamic Programming (DP) on Grids:__

Grid problems often exhibit overlapping subproblems. This means that the

solution for a larger problem depends on the solutions to smalleroptimalwithin the grid. A naive approach would involve calculating the solutions for each cellsubproblems, leading to significant redundancy and inefficiency. Whereas DP uses theindependentlyproperty by storing solutions to previously encountered subproblems, significantly reducing the number of repeated calculations and leading to a more efficient solution.overlapping subproblems

__Use Cases of Dynamic Programming (DP) on Grids:__

__1. Count number of Paths from the Top-Left Cell to the Bottom-Right cell of the grid:__

The problem is we are given a grid of size ** M X N **and we need to count all unique possible paths from the

**to the**

**top left****cell with the constraints that from each cell you can either move only to the**

**bottom right****or**

**right****. We can solve this by maintaining a 2D array**

**down****of size M X N such that count[i][j] will denote the number of unique paths to reach cell (i, j) starting from (0, 0). We can initialize the first row and the first columns with all ones and then iterate over all the cells updating**

**count[][]****by**

**count[i][j]****and**

**count[i-1][j]**

**count[i][j-1].**__2. Count number of Paths from the Top-Left Cell to the Bottom Right cell of the grid with obstructions:__

The problem is we are given a grid of size ** M X N **where some of the cells are

**and we need to count all unique possible paths from the**

**blocked,****to the**

**top left****cell with the constraints that from each cell you can either move only to the**

**bottom right****or**

**right****and we can only move in the**

**down****cells. We can solve this similar to the above approach and keeping track of the blocked cells and initializing the count of paths to reach the blocked cells with 0.**

**unblocked**__3. Minimum Steps to reach any of the boundary edges of a matrix:__

The problem is we are given a grid of size ** N X M**, where

**a**

_{i, j}**denotes the cell is not empty,**

**Â = 1**

**a**

_{i, j}**denotes the cell is empty, and**

**Â = 0**

**a**

_{i, j}**, denotes that you are**

**Â = 2****at that cell. You can move vertically**

**standing****or**

**up****and horizontally**

**down****or**

**left****to any cell which is empty. The task is to find the minimum number of steps to reach any boundary edge of the matrix. We can solve this problem by maintaining a 2D arrayÂ**

**right****which stores the minimum number of steps to reach any index i, j andÂ**

**dp[][]Â****Â to store if any particular i, j position has been visited or not previously.**

**vis[][]**__4. Minimum Cost Path from the Top Left Cell to the Bottom Right Cell of the grid:__

The problem is we are given a cost matrix ** cost[][]** and a position

**in cost[][], find the**

**(M, N)****to reach (M, N) from (0, 0). Each cell of the matrix represents a cost to traverse through that cell. The total cost of a path to reach (M, N) is the**

**minimum cost****of all the costs on that path (including both source and destination). You can only traverse down, right and diagonally lower cells from a given cell, i.e., from a given cell**

**sum****, cells**

**(i, j)****,**

**(i+1, j)****, and**

**(i, j+1)****can be traversed.Â We can solve the problem by maintaining a**

**(i+1, j+1)****array**

**2D****such that dp[i][j] denotes the**

**dp[][]****to reach cell (i, j) from cell (0, 0). We can initialize the first row and column by taking the prefix sums from (0,0) to the particular cell and then iterate over the remaining cells updating dp[i][j] using**

**min cost****,**

**dp[i-1][j]****and**

**dp[i][j-1]****.**

**dp[i-1][j-1]**__5. Minimum Path Sum in a triangle:__

The problem is we are given a triangular structure of numbers, find the ** minimum **path sum from top to bottom. Each step you may move to adjacent numbers on the row below. We can solve the problem by maintaining a

**array**

**2D****such that dp[i][j] denotes the**

**dp[][]****to reach cell (i, j) from the top of the triangle. We can initialize the first row and column by taking the prefix sums from (0,0) to the particular cell and then iterate over the remaining cells updating dp[i][j] using**

**min cost****and**

**dp[i-1][j]****.**

**dp[i-1][j-1]**__6. Maximum Path Sum from the first row to the last row of the grid:__

The problem is we are given a matrix of N * M and we have to find the maximum path sum in matrix starting from any cell in the first row and ending at any cell in the last row. The maximum path is sum of all elements from first row to last row where you are allowed to move only down or diagonally to left or right. You can start from any element in first row. The problem can be solved using a ** 2D** array

**such that**

**dp[][]****denotes the**

**dp[i][j]****to reach cell (i, j) from any of the cell from the first row. We can initialize the first row and column with the same value as the input matrix and then iterate over the remaining cells updating dp[i][j] using**

**min cost****,**

**dp[i-1][j]****and**

**dp[i-1][j-1]****.**

**dp[i-1][j+1]**__7. Finding Maximum Size Square Sub-matrix with all 1s:__

The problem is we are given a binary matrix, and we need to find out the maximum size square sub-matrix with all 1s.Â The problem can be solved by using a ** 2D** matrix

**such that**

**dp[][]****represents the size of the square sub-matrix with all 1s including M[i][j] where M[i][j] is the rightmost and bottom-most entry in sub-matrix. We can then iterate over the cells updating dp[][] as**

**dp[i][j]****.**

**dp[i][j] = min(dp[i][j-1], dp[i-1][j], dp[i-1][j-1]) + 1**__8. Finding Maximum Sum Rectangle in a 2D Grid:__

The problem is we are given a 2D array and we need to find the maximum sum submatrix in it. We can solve the problem in O(N^3) where we basically find top and bottom row numbers (which have maximum sum) for every fixed left and right column pair. To find the top and bottom row numbers, calculate the sum of elements in every row from left to right and store these sums in an array say temp[]. Then, we apply ** Kadaneâ€™s** 1D algorithm on temp[], and get the maximum sum subarray of temp, this maximum sum would be the maximum possible sum with left and right as boundary columns.

__9. Number of ways to reach the destination in the grid with exactly K coins (3D DP):__

The problem is we are given a matrix where every cell has some number of coins, and we have to find the count number of ways to reach bottom right from top left with exactly k coins. We can move to (i+1, j) and (i, j+1) from a cell (i, j). We can solve this by maintaining a ** 3D **array

**such that**

**dp[][][]****stores the**

**dp[i][j][k]****to reach cell (i, j) starting from (0, 0) with k coins.**

**number of ways**__10. Maximum objects to collect starting from top left and top right cells of the grid (3D DP):__

The problem is that we have placed two robots, one at the top left and other at the top right cell simultaneously and from a cellÂ `(i, j)`

, the robot can move to cellÂ `(i + 1, j - 1)`

,Â `(i + 1, j)`

, orÂ `(i + 1, j + 1)`

and we need to find the maximum number of objects the robots can collect till they reach the last row. The problem can be solved by maintaining a ** 3D** array

**, such that**

**dp[][][]****stores the**

**dp[i][j][k]****objects collected when both the robots are in the ith row with first robot in jth column and second robot in kth column.**

**maximum**__11. Maximum objects to collect starting from top left to bottom right cell and then from bottom right back to top left cell of the grid (3D DP):__

The problem is that we are given a 2D MatrixÂ ** grid[][]**Â of NxN size, some cells are blocked, and the remaining unblocked cells have chocolate in them. and we need to find the maximum number of chocolates that you can collect by moving from cellÂ

**Â to cellÂ**

**(0, 0)****Â and then back to cellÂ**

**(N-1, N-1)****. While moving from cellÂ**

**(0, 0)****Â to cellÂ**

**(0, 0)****, only allowed moves are:**

**(N-1, N-1)****Â (i, j) to (i+1, j)Â**

**Â Move down****Â (i, j) to (i, j+1) and while moving back from cell**

**or Move Right****Â to cellÂ**

**Â (N-1, N-1)****, only allowed moves are:Â**

**(0, 0)****Â (i, j) to (i-1, j) orÂ**

**Move up****Â (i, j) to (i, j-1). The problem can be solved using the following approach: Instead of walking from end to beginning, letâ€™s reverse the second leg of the path, so we can consider two persons both moving from the beginning to the end. Now, instead of considering both the pathsÂ**

**Move left****we need to move both the personsÂ**

**independentlyÂ****to maximize the number of chocolates.Â Maintain a**

**simultaneouslyÂ****DP array**

**4D****, such that**

**dp[][][][]****stores the**

**dp[r1][c1][r2][c2]****number of chocolates we can have if the first person has reached cell**

**maximum****and the second person has reached cell**

**(r1, c1)****. We can reduce the DP states to get a 3D DP array.**

**(r2, c2)**__Practice Problems on Dynamic Programming (DP) on Grids:__

Problem |
Problem Link |
---|---|

Practice Now |