# Collect maximum coins before hitting a dead end

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.
3. 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.
2. 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.

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.

## C++

 `// A Naive Recursive C++ program to find maximum number of coins ` `// that can be collected before hitting a dead end ` `#include ` `using` `namespace` `std; ` `#define R 5 ` `#define C 5 ` ` `  `// to check whether current cell is out of the grid or not ` `bool` `isValid(``int` `i, ``int` `j) ` `{ ` `    ``return` `(i >=0 && i < R && j >=0 && j < C); ` `} ` ` `  `// dir = 0 for left, dir = 1 for facing right.  This function returns ` `// number of maximum coins that can be collected starting from (i, j). ` `int` `maxCoinsRec(``char` `arr[R][C],  ``int` `i, ``int` `j, ``int` `dir) ` `{ ` `    ``// If this is a invalid cell or if cell is a blocking cell ` `    ``if` `(isValid(i,j) == ``false` `|| arr[i][j] == ``'#'``) ` `        ``return` `0; ` ` `  `    ``// Check if this cell contains the coin 'C' or if its empty 'E'. ` `    ``int` `result = (arr[i][j] == ``'C'``)? 1: 0; ` ` `  `    ``// Get the maximum of two cases when you are facing right in this cell ` `    ``if` `(dir == 1) ``// Direction is right ` `       ``return` `result + max(maxCoinsRec(arr, i+1, j, 0),     ``// Down ` `                             ``maxCoinsRec(arr, i, j+1, 1));  ``// Ahead in right ` ` `  `    ``// Direction is left ` `    ``// Get the maximum of two cases when you are facing left in this cell ` `     ``return`  `result + max(maxCoinsRec(arr, i+1, j, 1),    ``// Down ` `                           ``maxCoinsRec(arr, i, j-1, 0));  ``// Ahead in left ` `} ` ` `  `// Driver program to test above function ` `int` `main() ` `{ ` `    ``char` `arr[R][C] = { {``'E'``, ``'C'``, ``'C'``, ``'C'``, ``'C'``}, ` `                       ``{``'C'``, ``'#'``, ``'C'``, ``'#'``, ``'E'``}, ` `                       ``{``'#'``, ``'C'``, ``'C'``, ``'#'``, ``'C'``}, ` `                       ``{``'C'``, ``'E'``, ``'E'``, ``'C'``, ``'E'``}, ` `                       ``{``'C'``, ``'E'``, ``'#'``, ``'C'``, ``'E'``} ` `                     ``}; ` ` `  `   ``// As per the question initial cell is (0, 0) and direction is ` `    ``// right ` `    ``cout << ``"Maximum number of collected coins is "` `         ``<< maxCoinsRec(arr, 0, 0, 1); ` ` `  `    ``return` `0; ` `} `

## Python3

 `# A Naive Recursive Python 3 program to  ` `# find maximum number of coins  ` `# that can be collected before hitting a dead end  ` `R``=` `5` `C``=` `5` ` `  `# to check whether current cell is out of the grid or not  ` `def` `isValid( i, j):  ` ` `  `    ``return` `(i >``=``0` `and` `i < R ``and` `j >``=``0` `and` `j < C)  ` ` `  ` `  `# dir = 0 for left, dir = 1 for facing right.  ` `# This function returns  ` `# number of maximum coins that can be collected ` `# starting from (i, j).  ` `def` `maxCoinsRec(arr, i, j, ``dir``):  ` ` `  `    ``# If this is a invalid cell or if cell is a blocking cell  ` `    ``if` `(isValid(i,j) ``=``=` `False` `or` `arr[i][j] ``=``=` `'#'``):  ` `        ``return` `0` ` `  `    ``# Check if this cell contains the coin 'C' or if its empty 'E'.  ` `    ``if` `(arr[i][j] ``=``=` `'C'``): ` `        ``result``=``1` `    ``else``: ` `        ``result``=``0` ` `  `    ``# Get the maximum of two cases when you are facing right in this cell  ` `    ``if` `(``dir` `=``=` `1``): ` `         `  `     ``# Direction is right  ` `        ``return` `(result ``+` `max``(maxCoinsRec(arr, i``+``1``, j, ``0``), ` `               ``maxCoinsRec(arr, i, j``+``1``, ``1``)))  ` ` `  `    ``# Direction is left  ` `    ``# Get the maximum of two cases when you are facing left in this cell  ` `    ``return` `(result ``+` `max``(maxCoinsRec(arr, i``+``1``, j, ``1``), ` `           ``maxCoinsRec(arr, i, j``-``1``, ``0``)))  ` ` `  ` `  `# Driver program to test above function  ` `if` `__name__``=``=``'__main__'``: ` `    ``arr ``=` `[ [``'E'``, ``'C'``, ``'C'``, ``'C'``, ``'C'``], ` `          ``[``'C'``, ``'#'``, ``'C'``, ``'#'``, ``'E'``], ` `          ``[``'#'``, ``'C'``, ``'C'``, ``'#'``, ``'C'``], ` `          ``[``'C'``, ``'E'``, ``'E'``, ``'C'``, ``'E'``], ` `          ``[``'C'``, ``'E'``, ``'#'``, ``'C'``, ``'E'``] ]  ` ` `  `    ``# As per the question initial cell is (0, 0) and direction is  ` `    ``# right  ` `    ``print``(``"Maximum number of collected coins is "``, maxCoinsRec(arr, ``0``, ``0``, ``1``))  ` ` `  `# this code is contributed by ash264 `