|
1 | | -// Problem(3-D DP) : We are given an ‘N *M’ matrix.Every cell of the matrix has some chocolates on it, mat[i][j] gives us the |
2 | | -// number of chocolates.We have two friends ‘Alice’ and ‘Bob’.initially, Alice is standing on the cell(0, 0) and Bob is standing |
3 | | -// 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(↓), |
4 | | -// to the bottom - right cell(↘), or to the bottom - left cell(↙). When Alica and Bob visit a cell, they take all the chocolates |
5 | | -// from that cell with them. It can happen that they visit the same cell, in that case, the chocolates need to be considered only |
6 | | -// once. They cannot go out of the boundary of the given matrix, we need to return the maximum number of chocolates that Bob and |
7 | | -// Alice can together collect. |
| 1 | +// Problem(3-D DP) : https://www.codingninjas.com/codestudio/problems/ninja-and-his-friends_3125885 |
| 2 | + |
| 3 | +// Sample Inputs |
| 4 | +// 3 4 |
| 5 | +// 2 3 1 2 |
| 6 | +// 3 4 2 2 |
| 7 | +// 5 6 3 5 |
| 8 | +// 2 2 |
| 9 | +// 1 1 |
| 10 | +// 1 2 |
| 11 | + |
| 12 | +// Corresponding Outputs |
| 13 | +// 21 |
| 14 | +// 5 |
8 | 15 |
|
9 | 16 | #include <bits/stdc++.h>
|
10 | 17 | using namespace std;
|
11 | 18 |
|
| 19 | +// recursive function |
12 | 20 | int f(int i, int j1, int j2, vector<vector<int>> &grid, int n, int m, vector<vector<vector<int>>> &dp)
|
13 | 21 | {
|
14 | 22 | if (i == n)
|
@@ -76,6 +84,7 @@ int f(int i, int j1, int j2, vector<vector<int>> &grid, int n, int m, vector<vec
|
76 | 84 | }
|
77 | 85 | }
|
78 | 86 |
|
| 87 | +// function returning answer |
79 | 88 | int maximumChocolates(int r, int c, vector<vector<int>> &grid)
|
80 | 89 | {
|
81 | 90 | vector<vector<vector<int>>> dp(r, vector<vector<int>>(c, vector<int>(c)));
|
@@ -106,20 +115,3 @@ int main()
|
106 | 115 | }
|
107 | 116 | cout << maximumChocolates(r, c, grid);
|
108 | 117 | }
|
109 | | - |
110 | | -// Sample Inputs |
111 | | - |
112 | | -// 3 4 |
113 | | -// 2 3 1 2 |
114 | | -// 3 4 2 2 |
115 | | -// 5 6 3 5 |
116 | | - |
117 | | -// 2 2 |
118 | | -// 1 1 |
119 | | -// 1 2 |
120 | | - |
121 | | -// Corresponding Outputs |
122 | | - |
123 | | -// 21 |
124 | | - |
125 | | -// 5 |
0 commit comments