forked from codemistic/Data-Structures-and-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathninjaAndHisFriends.cpp
125 lines (115 loc) · 3.68 KB
/
ninjaAndHisFriends.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// 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
// 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.
#include <bits/stdc++.h>
using namespace std;
int f(int i, int j1, int j2, vector<vector<int>> &grid, int n, int m, vector<vector<vector<int>>> &dp)
{
if (i == n)
{
return 0;
}
if (dp[i][j1][j2] != -1)
{
return dp[i][j1][j2];
}
int mx = INT_MIN;
// if Alice can move to the left column in the row below
if (((j1 - 1) >= 0))
{
// if Bob can move to the same column in the row below
mx = max(mx, f(i + 1, j1 - 1, j2, grid, n, m, dp));
// if Bob can move to the left column in the row below
if ((j2 - 1) >= 0)
{
mx = max(mx, f(i + 1, j1 - 1, j2 - 1, grid, n, m, dp));
}
// if Bob can move to the right column in the row below
if ((j2 + 1) < m)
{
mx = max(mx, f(i + 1, j1 - 1, j2 + 1, grid, n, m, dp));
}
}
// if Alice can move to the same column in the row below
// if Bob can move to the same column in the row below
mx = max(mx, f(i + 1, j1, j2, grid, n, m, dp));
// if Bob can move to the left column in the row below
if ((j2 - 1) >= 0)
{
mx = max(mx, f(i + 1, j1, j2 - 1, grid, n, m, dp));
}
// if Bob can move to the right column in the row below
if ((j2 + 1) < m)
{
mx = max(mx, f(i + 1, j1, j2 + 1, grid, n, m, dp));
}
// if Alice can move to the right column in the row below
if (((j1 + 1) < m))
{
// if Bob can move to the same column in the row below
mx = max(mx, f(i + 1, j1 + 1, j2, grid, n, m, dp));
// if Bob can move to the left column in the row below
if ((j2 - 1) >= 0)
{
mx = max(mx, f(i + 1, j1 + 1, j2 - 1, grid, n, m, dp));
}
// if Bob can move to the right column in the row below
if ((j2 + 1) < m)
{
mx = max(mx, f(i + 1, j1 + 1, j2 + 1, grid, n, m, dp));
}
}
if (j1 == j2)
{
// if same cell for both alice and bob, count chocolates only once
return dp[i][j1][j2] = (mx + grid[i][j1]);
}
else
{
return dp[i][j1][j2] = (mx + grid[i][j1] + grid[i][j2]);
}
}
int maximumChocolates(int r, int c, vector<vector<int>> &grid)
{
vector<vector<vector<int>>> dp(r, vector<vector<int>>(c, vector<int>(c)));
for (int i = 0; i < r; i++)
{
for (int j = 0; j < c; j++)
{
for (int k = 0; k < c; k++)
{
dp[i][j][k] = -1;
}
}
}
return f(0, 0, c - 1, grid, r, c, dp);
}
int main()
{
int r, c;
cin >> r >> c;
vector<vector<int>> grid(r, vector<int>(c));
for (int i = 0; i < r; i++)
{
for (int j = 0; j < c; j++)
{
cin >> grid[i][j];
}
}
cout << maximumChocolates(r, c, grid);
}
// Sample Inputs
// 3 4
// 2 3 1 2
// 3 4 2 2
// 5 6 3 5
// 2 2
// 1 1
// 1 2
// Corresponding Outputs
// 21
// 5