-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path14_CherryPickupI.cpp
237 lines (203 loc) · 6.08 KB
/
14_CherryPickupI.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
// https://leetcode.com/problems/cherry-pickup/
// You are given an n x n grid representing a field of cherries,
// each cell is one of three possible integers.
// 0 means the cell is empty, so you can pass through,
// 1 means the cell contains a cherry that you can pick up and
// pass through, or
// -1 means the cell contains a thorn that blocks your way.
// Return the maximum number of cherries you can collect by
// following the rules below:
// Starting at the position (0, 0) and reaching (n - 1, n - 1)
// by moving right or down through valid path cells (cells with value 0 or 1).
// After reaching (n - 1, n - 1), returning to (0, 0) by moving
// left or up through valid path cells.
// When passing through a path cell containing a cherry, you pick
// it up, and the cell becomes an empty cell 0.
// If there is no valid path between (0, 0) and (n - 1, n - 1),
// then no cherries can be collected.
#include <bits/stdc++.h>
using namespace std;
class Solution2
{
// Recursive solution : Memoization // Wrong Solution
public:
int cherryPickup(vector<vector<int>> &grid)
{
int m = grid.size(), n = grid[0].size();
return max(reachEnd(grid), 0);
}
int reachEnd(vector<vector<int>> &grid, int i = 0, int j = 0)
{
int flag = 0;
if (i >= grid.size() || j >= grid[0].size() || grid[i][j] == -1)
return -(1e9);
if (grid[i][j] == 1)
grid[i][j] = 0, flag = 1;
if (i == grid.size() - 1 && j == grid[0].size() - 1)
{
// reached first destination
vector<vector<int>> cp(grid.size() - 1, vector<int>(grid[0].size() - 1, -1));
return flag + reachBegin(grid, cp, grid.size() - 1, grid[0].size() - 1);
}
// Go right
int r = flag + reachEnd(grid, i, j + 1);
// Go down
int d = flag + reachEnd(grid, i + 1, j);
if (flag)
grid[i][j] = 1;
return max(r, d);
}
int reachBegin(vector<vector<int>> &grid, vector<vector<int>> &cp, int i = 0, int j = 0)
{
int flag = 0;
if (i < 0 || j < 0 || grid[i][j] == -1)
return -1e9;
if (cp[i][j] != -1)
return cp[i][j];
if (grid[i][j] == 1)
grid[i][j] = 0, flag = 1;
if (i == 0 && j == 0)
{
// reached final destination
return flag;
}
// Go left
int l = flag + reachBegin(grid, cp, i, j - 1);
// Go up
int u = flag + reachBegin(grid, cp, i - 1, j);
if (flag)
grid[i][j] = 1;
return cp[i][j] = max(l, u);
}
};
class Solution1
{
// Better recursive solution
public:
int cherryPickup(vector<vector<int>> &grid)
{
int m = grid.size(), n = grid[0].size();
return max(0, reachEnd(grid));
}
int reachEnd(vector<vector<int>> &grid, int i = 0, int j = 0)
{
int flag = 0;
if (i >= grid.size() || j >= grid[0].size() || grid[i][j] == -1)
return -(1e9);
if (grid[i][j] == 1)
grid[i][j] = 0, flag = 1;
if (i == grid.size() - 1 && j == grid[0].size() - 1)
{
// reached first destination
return flag + reachBegin(grid, grid.size() - 1, grid[0].size() - 1);
}
// Go right
int r = flag + reachEnd(grid, i, j + 1);
// Go down
int d = flag + reachEnd(grid, i + 1, j);
if (flag)
grid[i][j] = 1;
return max(r, d);
}
int reachBegin(vector<vector<int>> &grid, int i = 0, int j = 0)
{
int flag = 0;
if (i < 0 || j < 0 || grid[i][j] == -1)
return -1e9;
if (grid[i][j] == 1)
grid[i][j] = 0, flag = 1;
if (i == 0 && j == 0)
{
// reached final destination
return flag;
}
// Go left
int l = flag + reachBegin(grid, i, j - 1);
// Go up
int u = flag + reachBegin(grid, i - 1, j);
if (flag)
grid[i][j] = 1;
return max(l, u);
}
};
class Solution
{
// Recursive solution
public:
int cherryPickup(vector<vector<int>> &grid)
{
int m = grid.size(), n = grid[0].size();
int ans = INT_MIN, curr = 0;
reachEnd(grid, ans, curr);
return (ans == INT_MIN ? 0 : ans);
}
void reachEnd(vector<vector<int>> &grid, int &ans, int &curr, int i = 0, int j = 0)
{
int flag = 0;
if (i >= grid.size() || j >= grid[0].size() || grid[i][j] == -1)
{
return;
}
if (i == grid.size() - 1 && j == grid[0].size() - 1)
{
// reached destination
reachBegin(grid, ans, curr, grid.size() - 1, grid[0].size() - 1);
return;
}
if (grid[i][j] == 1)
{
++curr;
grid[i][j] = 0;
flag = 1;
}
// Go right
reachEnd(grid, ans, curr, i, j + 1);
// Go down
reachEnd(grid, ans, curr, i + 1, j);
if (grid[i][j] == 0 && flag)
{
--curr;
grid[i][j] = 1;
}
}
void reachBegin(vector<vector<int>> &grid, int &ans, int &curr, int i = 0, int j = 0)
{
int flag = 0;
if (i < 0 || j < 0 || grid[i][j] == -1)
{
return;
}
if (i == 0 && j == 0)
{
// reached destination
ans = max(ans, curr);
return;
}
if (grid[i][j] == 1)
{
++curr;
grid[i][j] = 0;
flag = 1;
}
// Go left
reachBegin(grid, ans, curr, i, j - 1);
// Go up
reachBegin(grid, ans, curr, i - 1, j);
if (grid[i][j] == 0 && flag)
{
--curr;
grid[i][j] = 1;
}
}
};
int main()
{
vector<vector<int>> grid = {{0, 1, -1}, {1, 0, -1}, {1, 1, 1}};
vector<vector<int>> grid1 = {{1, 1, -1}, {1, -1, 1}, {-1, 1, 1}};
// [[1,1,-1],[1,-1,1],[-1,1,1]]
Solution2 Obj1;
cout << Obj1.cherryPickup(grid);
ios_base::sync_with_stdio(false);
cin.tie(NULL);
return 0;
}