-
Notifications
You must be signed in to change notification settings - Fork 46
/
_2556.cpp
57 lines (53 loc) · 2.08 KB
/
_2556.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
/**
* LeetCode 2556 - Disconnect Path in a Binary Matrix by at Most One Flip
*
* DP approach to count # of distinct paths
*
* 1. First check whether there exist one path.
* 2. If so, then the question boils down to identify a cut-vertex of the graph.
* 3. A simple way to tell whether (i, j) is a cut-vertex is by counting # of
* distinct 1-path from (0, 0) to (i, j) and from (i, j) to (r - 1, c - 1).
* By rule of product, if the product of these two counts is equal to the
* total distinct # of 1-path from (0, 0) to (r - 1, c - 1), then it is a
* cut-vertex; vice versa.
* 4. The overall count can be very large. One possible way is to do the calculation
* modulo some big prime number.
*/
class Solution {
public:
bool isPossibleToCutPath(vector<vector<int>>& grid) {
long long mod = 1000000007;
int r = grid.size(), c = grid[0].size();
vector<vector<long long>> f(r + 2, vector<long long>(c + 2));
vector<vector<long long>> g(r + 2, vector<long long>(c + 2));
if (grid[0][0] == 0 || grid[r - 1][c - 1] == 0)
return true;
f[1][1] = 1;
for (int i=1; i<=r; i++)
for (int j=1; j<=c; j++) {
if (i == 1 && j == 1) continue;
if (grid[i-1][j-1] == 1)
f[i][j] = (f[i-1][j] + f[i][j-1]) % mod;
}
g[r][c] = 1;
for (int i=r; i>=1; i--)
for (int j=c; j>=1; j--) {
if (i == r && j == c) continue;
if (grid[i-1][j-1] == 1)
g[i][j] = (g[i+1][j] + g[i][j+1]) % mod;
}
if (f[r][c] == 0)
return true;
assert (f[r][c] == g[1][1]);
for (int i=1; i<=r; i++)
for (int j=1; j<=c; j++) {
if (i == 1 && j == 1) continue;
if (i == r && j == c) continue;
long long tmp = f[i][j] * g[i][j] % mod;
if (tmp == f[r][c]) {
return true;
}
}
return false;
}
};