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 40b642a

Browse files
ninjaAndHisFriends.cpp created
1 parent 98d303f commit 40b642a

File tree

1 file changed

+125
-0
lines changed

1 file changed

+125
-0
lines changed
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
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.
8+
9+
#include <bits/stdc++.h>
10+
using namespace std;
11+
12+
int f(int i, int j1, int j2, vector<vector<int>> &grid, int n, int m, vector<vector<vector<int>>> &dp)
13+
{
14+
if (i == n)
15+
{
16+
return 0;
17+
}
18+
if (dp[i][j1][j2] != -1)
19+
{
20+
return dp[i][j1][j2];
21+
}
22+
int mx = INT_MIN;
23+
// if Alice can move to the left column in the row below
24+
if (((j1 - 1) >= 0))
25+
{
26+
// if Bob can move to the same column in the row below
27+
mx = max(mx, f(i + 1, j1 - 1, j2, grid, n, m, dp));
28+
// if Bob can move to the left column in the row below
29+
if ((j2 - 1) >= 0)
30+
{
31+
mx = max(mx, f(i + 1, j1 - 1, j2 - 1, grid, n, m, dp));
32+
}
33+
// if Bob can move to the right column in the row below
34+
if ((j2 + 1) < m)
35+
{
36+
mx = max(mx, f(i + 1, j1 - 1, j2 + 1, grid, n, m, dp));
37+
}
38+
}
39+
// if Alice can move to the same column in the row below
40+
// if Bob can move to the same column in the row below
41+
mx = max(mx, f(i + 1, j1, j2, grid, n, m, dp));
42+
// if Bob can move to the left column in the row below
43+
if ((j2 - 1) >= 0)
44+
{
45+
mx = max(mx, f(i + 1, j1, j2 - 1, grid, n, m, dp));
46+
}
47+
// if Bob can move to the right column in the row below
48+
if ((j2 + 1) < m)
49+
{
50+
mx = max(mx, f(i + 1, j1, j2 + 1, grid, n, m, dp));
51+
}
52+
// if Alice can move to the right column in the row below
53+
if (((j1 + 1) < m))
54+
{
55+
// if Bob can move to the same column in the row below
56+
mx = max(mx, f(i + 1, j1 + 1, j2, grid, n, m, dp));
57+
// if Bob can move to the left column in the row below
58+
if ((j2 - 1) >= 0)
59+
{
60+
mx = max(mx, f(i + 1, j1 + 1, j2 - 1, grid, n, m, dp));
61+
}
62+
// if Bob can move to the right column in the row below
63+
if ((j2 + 1) < m)
64+
{
65+
mx = max(mx, f(i + 1, j1 + 1, j2 + 1, grid, n, m, dp));
66+
}
67+
}
68+
if (j1 == j2)
69+
{
70+
// if same cell for both alice and bob, count chocolates only once
71+
return dp[i][j1][j2] = (mx + grid[i][j1]);
72+
}
73+
else
74+
{
75+
return dp[i][j1][j2] = (mx + grid[i][j1] + grid[i][j2]);
76+
}
77+
}
78+
79+
int maximumChocolates(int r, int c, vector<vector<int>> &grid)
80+
{
81+
vector<vector<vector<int>>> dp(r, vector<vector<int>>(c, vector<int>(c)));
82+
for (int i = 0; i < r; i++)
83+
{
84+
for (int j = 0; j < c; j++)
85+
{
86+
for (int k = 0; k < c; k++)
87+
{
88+
dp[i][j][k] = -1;
89+
}
90+
}
91+
}
92+
return f(0, 0, c - 1, grid, r, c, dp);
93+
}
94+
95+
int main()
96+
{
97+
int r, c;
98+
cin >> r >> c;
99+
vector<vector<int>> grid(r, vector<int>(c));
100+
for (int i = 0; i < r; i++)
101+
{
102+
for (int j = 0; j < c; j++)
103+
{
104+
cin >> grid[i][j];
105+
}
106+
}
107+
cout << maximumChocolates(r, c, grid);
108+
}
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 によって変換されたページ (->オリジナル) /