-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchocolatePickup.cpp
More file actions
69 lines (57 loc) · 2.44 KB
/
chocolatePickup.cpp
File metadata and controls
69 lines (57 loc) · 2.44 KB
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
#include <iostream>
using namespace std;
int maximumChocolates(int n, int m, vector<vector<int>> &grid) {
// Create two 2D vectors 'front' and 'cur' to store computed results
vector<vector<int>> front(m, vector<int>(m, 0));
vector<vector<int>> cur(m, vector<int>(m, 0));
// Initialize 'front' for the last row
for (int j1 = 0; j1 < m; j1++) {
for (int j2 = 0; j2 < m; j2++) {
if (j1 == j2)
front[j1][j2] = grid[n - 1][j1];
else
front[j1][j2] = grid[n - 1][j1] + grid[n - 1][j2];
}
}
// Outer nested loops for traversing the DP array from the second-to-last row up to the first row
for (int i = n - 2; i >= 0; i--) {
for (int j1 = 0; j1 < m; j1++) {
for (int j2 = 0; j2 < m; j2++) {
int maxi = INT_MIN;
// Inner nested loops to try out 9 options (diagonal moves)
for (int di = -1; di <= 1; di++) {
for (int dj = -1; dj <= 1; dj++) {
int ans;
if (j1 == j2)
ans = grid[i][j1];
else
ans = grid[i][j1] + grid[i][j2];
// Check if the move is valid (within the grid boundaries)
if ((j1 + di < 0 || j1 + di >= m) || (j2 + dj < 0 || j2 + dj >= m))
ans += -1e9; // A very large negative value to represent an invalid move
else
ans += front[j1 + di][j2 + dj]; // Include the value from the 'front' array
maxi = max(ans, maxi); // Update the maximum result
}
}
cur[j1][j2] = maxi; // Store the maximum result for this state in the 'cur' array
}
}
front = cur; // Update 'front' with the values from 'cur' for the next iteration
}
// The maximum chocolates that can be collected is stored at the top-left corner of the 'front' array
return front[0][m - 1];
}
int main() {
// Define the grid as a 2D vector
vector<vector<int>> matrix{
{2, 3, 1, 2},
{3, 4, 2, 2},
{5, 6, 3, 5},
};
int n = matrix.size(); // Number of rows
int m = matrix[0].size(); // Number of columns
// Call the maximumChocolates function and print the result
cout << maximumChocolates(n, m, matrix);
return 0;
}