VOOZH about

URL: https://www.geeksforgeeks.org/dsa/collect-maximum-coins-before-hitting-a-dead-end/

⇱ Collect maximum coins before hitting a dead end - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Collect maximum coins before hitting a dead end

Last Updated : 27 Mar, 2023

Given a character matrix where every cell has one of the following values.

'C' --> This cell has coin

'#' --> This cell is a blocking cell. 
 We can not go anywhere from this.

'E' --> This cell is empty. We don't get
 a coin, but we can move from here. 

Initial position is cell (0, 0) and initial direction is right.

Following are rules for movements across cells.

If face is Right, then we can move to below cells

  1. Move one step ahead, i.e., cell (i, j+1) and direction remains right.
  2. Move one step down and face left, i.e., cell (i+1, j) and direction becomes left.

If face is Left, then we can move to below cells

  1. Move one step ahead, i.e., cell (i, j-1) and direction remains left.

Move one step down and face right, i.e., cell (i+1, j) and direction becomes right.
Final position can be anywhere and final direction can also be anything. The target is to collect maximum coins.

Example:

👁 Image
 

We strongly recommend you to minimize your browser and try this yourself first.

The above problem can be recursively defined as below:

maxCoins(i, j, d):  Maximum number of coins that can be 
                   collected if we begin at cell (i, j)
                   and direction d.
                   d can be either 0 (left) or 1 (right)

  // If this is a blocking cell, return 0. isValid() checks
  // if i and j are valid row and column indexes.
  If (arr[i][j] == '#' or isValid(i, j) == false)
      return 0

  // Initialize result
  If (arr[i][j] == 'C')
      result = 1;
  Else 
      result = 0;

  If (d == 0) // Left direction 
      return result +  max(maxCoins(i+1, j, 1),  // Down
                           maxCoins(i, j-1, 0)); // Ahead in left

  If (d == 1) // Right direction 
      return result +  max(maxCoins(i+1, j, 1),  // Down
                           maxCoins(i, j+1, 0)); // Ahead in right


Below is C++ implementation of above recursive algorithm.


Output
Maximum number of collected coins is 8

Time Complexity: O(2^(R+C)), where R and C are the number of rows and columns in the grid, respectively. This is because in the worst case, we need to explore all possible paths in the grid, which can be as many as 2^(R+C).
Auxiliary Space: O(R+C), which corresponds to the maximum depth of the recursive call stack.

We can solve this problem in Polynomial Time using Dynamic Programming. The idea is to use a 3 dimensional table dp[R][C][k] where R is number of rows, C is number of columns and d is direction. Below is Dynamic Programming based C++ implementation.


Output
Maximum number of collected coins is 8

Time Complexity: O(R x C x d). Since d is 2, time complexity can be written as O(R x C).
Auxiliary Space: O(R x C x 2), We are using a 3D array of size R*C*2 to store the results of the subproblems, where R and C are the number of rows and columns of the grid.

Thanks to Gaurav Ahirwar for suggesting above solution.

Comment
Article Tags: