|
| 1 | +// Problem(3-D DP) : |
| 2 | + |
| 3 | +// We are given an ‘N *M’ matrix.Every cell of the matrix has some chocolates on it, mat[i][j] gives us the number of chocolates.We have two friends ‘Alice’ and ‘Bob’.initially, Alice is standing on the cell(0, 0) and Bob is standing on the cell(0, M - 1).Both of them can move only to the cells below them in these three directions: to the bottom cell(↓), to the bottom-right cell(↘), or to the bottom-left cell(↙). // When Alica and Bob visit a cell, they take all the chocolates from that cell with them. It can happen that they visit the same cell, in that case, the chocolates need to be considered only once. They cannot go out of the boundary of the given matrix, we need to return the maximum number of chocolates that Bob and Alice can together collect. |
| 4 | + |
| 5 | +/* Sample Inputs |
| 6 | + 3 4 |
| 7 | + 2 3 1 2 |
| 8 | + 3 4 2 2 |
| 9 | + 5 6 3 5 |
| 10 | + 2 2 |
| 11 | + 1 1 |
| 12 | + 1 2 |
| 13 | + |
| 14 | +Corresponding Outputs |
| 15 | + 21 |
| 16 | + 5 |
| 17 | + */ |
| 18 | + |
| 19 | +#include <bits/stdc++.h> |
| 20 | +using namespace std; |
| 21 | + |
| 22 | +// recursive function |
| 23 | +int f(int i, int j1, int j2, vector<vector<int>> &grid, int n, int m, vector<vector<vector<int>>> &dp) |
| 24 | +{ |
| 25 | + if (i == n) |
| 26 | + { |
| 27 | + return 0; |
| 28 | + } |
| 29 | + if (dp[i][j1][j2] != -1) |
| 30 | + { |
| 31 | + return dp[i][j1][j2]; |
| 32 | + } |
| 33 | + int mx = INT_MIN; |
| 34 | + // if Alice can move to the left column in the row below |
| 35 | + if (((j1 - 1) >= 0)) |
| 36 | + { |
| 37 | + // if Bob can move to the same column in the row below |
| 38 | + mx = max(mx, f(i + 1, j1 - 1, j2, grid, n, m, dp)); |
| 39 | + // if Bob can move to the left column in the row below |
| 40 | + if ((j2 - 1) >= 0) |
| 41 | + { |
| 42 | + mx = max(mx, f(i + 1, j1 - 1, j2 - 1, grid, n, m, dp)); |
| 43 | + } |
| 44 | + // if Bob can move to the right column in the row below |
| 45 | + if ((j2 + 1) < m) |
| 46 | + { |
| 47 | + mx = max(mx, f(i + 1, j1 - 1, j2 + 1, grid, n, m, dp)); |
| 48 | + } |
| 49 | + } |
| 50 | + // if Alice can move to the same column in the row below |
| 51 | + // if Bob can move to the same column in the row below |
| 52 | + mx = max(mx, f(i + 1, j1, j2, grid, n, m, dp)); |
| 53 | + // if Bob can move to the left column in the row below |
| 54 | + if ((j2 - 1) >= 0) |
| 55 | + { |
| 56 | + mx = max(mx, f(i + 1, j1, j2 - 1, grid, n, m, dp)); |
| 57 | + } |
| 58 | + // if Bob can move to the right column in the row below |
| 59 | + if ((j2 + 1) < m) |
| 60 | + { |
| 61 | + mx = max(mx, f(i + 1, j1, j2 + 1, grid, n, m, dp)); |
| 62 | + } |
| 63 | + // if Alice can move to the right column in the row below |
| 64 | + if (((j1 + 1) < m)) |
| 65 | + { |
| 66 | + // if Bob can move to the same column in the row below |
| 67 | + mx = max(mx, f(i + 1, j1 + 1, j2, grid, n, m, dp)); |
| 68 | + // if Bob can move to the left column in the row below |
| 69 | + if ((j2 - 1) >= 0) |
| 70 | + { |
| 71 | + mx = max(mx, f(i + 1, j1 + 1, j2 - 1, grid, n, m, dp)); |
| 72 | + } |
| 73 | + // if Bob can move to the right column in the row below |
| 74 | + if ((j2 + 1) < m) |
| 75 | + { |
| 76 | + mx = max(mx, f(i + 1, j1 + 1, j2 + 1, grid, n, m, dp)); |
| 77 | + } |
| 78 | + } |
| 79 | + if (j1 == j2) |
| 80 | + { |
| 81 | + // if same cell for both alice and bob, count chocolates only once |
| 82 | + return dp[i][j1][j2] = (mx + grid[i][j1]); |
| 83 | + } |
| 84 | + else |
| 85 | + { |
| 86 | + return dp[i][j1][j2] = (mx + grid[i][j1] + grid[i][j2]); |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +// function returning answer |
| 91 | +int maximumChocolates(int r, int c, vector<vector<int>> &grid) |
| 92 | +{ |
| 93 | + vector<vector<vector<int>>> dp(r, vector<vector<int>>(c, vector<int>(c))); |
| 94 | + for (int i = 0; i < r; i++) |
| 95 | + { |
| 96 | + for (int j = 0; j < c; j++) |
| 97 | + { |
| 98 | + for (int k = 0; k < c; k++) |
| 99 | + { |
| 100 | + dp[i][j][k] = -1; |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + return f(0, 0, c - 1, grid, r, c, dp); |
| 105 | +} |
| 106 | + |
| 107 | +int main() |
| 108 | +{ |
| 109 | + int r, c; |
| 110 | + cin >> r >> c; |
| 111 | + vector<vector<int>> grid(r, vector<int>(c)); |
| 112 | + for (int i = 0; i < r; i++) |
| 113 | + { |
| 114 | + for (int j = 0; j < c; j++) |
| 115 | + { |
| 116 | + cin >> grid[i][j]; |
| 117 | + } |
| 118 | + } |
| 119 | + cout << maximumChocolates(r, c, grid); |
| 120 | +} |
0 commit comments