Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 5366cb1

Browse files
changes made
1 parent 40b642a commit 5366cb1

File tree

1 file changed

+16
-24
lines changed

1 file changed

+16
-24
lines changed

‎24- Dynamic Programming/ninjaAndHisFriends.cpp

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,22 @@
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
815

916
#include <bits/stdc++.h>
1017
using namespace std;
1118

19+
// recursive function
1220
int f(int i, int j1, int j2, vector<vector<int>> &grid, int n, int m, vector<vector<vector<int>>> &dp)
1321
{
1422
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
7684
}
7785
}
7886

87+
// function returning answer
7988
int maximumChocolates(int r, int c, vector<vector<int>> &grid)
8089
{
8190
vector<vector<vector<int>>> dp(r, vector<vector<int>>(c, vector<int>(c)));
@@ -106,20 +115,3 @@ int main()
106115
}
107116
cout << maximumChocolates(r, c, grid);
108117
}
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

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /