-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path1706.cpp
More file actions
27 lines (27 loc) · 717 Bytes
/
1706.cpp
File metadata and controls
27 lines (27 loc) · 717 Bytes
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
class Solution {
public:
int M;
int N;
int dfs(vector<vector<int>>& grid, int x, int y) {
if (x == M) return y;
if (grid[x][y] == 1) {
if (y == N - 1) return -1;
if (grid[x][y + 1] == -1) return -1;
return dfs(grid, x + 1, y + 1);
}
else {
if (y == 0) return -1;
if (grid[x][y - 1] == 1) return -1;
return dfs(grid, x + 1, y - 1);
}
}
vector<int> findBall(vector<vector<int>>& grid) {
M = grid.size();
N = grid[0].size();
vector<int> res(N, 0);
for (int i = 0; i < N; i++) {
res[i] = dfs(grid, 0, i);
}
return res;
}
};