-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path2267.cpp
More file actions
59 lines (55 loc) · 1.68 KB
/
2267.cpp
File metadata and controls
59 lines (55 loc) · 1.68 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
class Solution {
public:
int M;
int N;
bool res = false;
vector<pair<int, int>> directions = {{0, 1}, {1, 0}};
vector<vector<vector<int>>> dp;
bool dfs(vector<vector<char>>& grid, int x, int y, int path) {
if (x == M - 1 && y == N - 1) {
if (path == 0) {
res = true;
dp[x][y][path] = true;
return true;
}
else return false;
}
if (res) return true;
if (dp[x][y][path] != -1) return dp[x][y][path];
vector<bool> v(2, false);
for (int i = 0; i < 2; i++) {
auto direction = directions[i];
int _x = x + direction.first;
int _y = y + direction.second;
if (_x < 0 || _x >= M || _y < 0 || _y >= N) {
v[i] = false;
continue;
}
if (grid[_x][_y] == ')') {
if (path == 0) {
v[i] = false;
continue;
}
else {
v[i] = dfs(grid, _x, _y, path - 1);
}
}
else {
v[i] = dfs(grid, _x, _y, path + 1);
}
}
dp[x][y][path] = (v[0] || v[1]);
return dp[x][y][path];
}
bool hasValidPath(vector<vector<char>>& grid) {
M = grid.size();
N = grid[0].size();
dp = vector(M, vector(N, vector<int>(M + N, -1)));
if ((M + N) % 2 == 0) return false;
if (grid[0][0] == ')') return false;
if (grid[M - 1][N - 1] == '(') return false;
int path = 1;
dfs(grid, 0, 0, path);
return res;
}
};