forked from mickey0524/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1020.Number-of-Enclaves.java
89 lines (78 loc) · 2.43 KB
/
1020.Number-of-Enclaves.java
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
// https://leetcode.com/problems/number-of-enclaves/
//
// algorithms
// Medium (53.34%)
// Total Accepted: 2,913
// Total Submissions: 5,461
class Solution {
public int numEnclaves(int[][] A) {
int row = A.length;
int col = A[0].length;
int res = 0;
ArrayList<int[]> path = new ArrayList<>();
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
if (A[i][j] == 1) {
path.clear();
this.recursive(A, path, i, j, row, col);
if (A[i][j] == 0 && path.size() > 0) {
res += path.size();
}
}
}
}
return res;
}
public void recursive(int[][] A, ArrayList<int[]> path, int x, int y, int row, int col) {
path.add(new int[]{x, y});
A[x][y] = 0;
if (x == 0 || x == row - 1 || y == 0 || y == col - 1 ||
A[x + 1][y] == -1 || A[x - 1][y] == - 1 || A[x][y + 1] == -1 || A[x][y - 1] == -1) {
for (int i = 0; i < path.size(); i++) {
A[path.get(i)[0]][path.get(i)[1]] = -1;
}
return;
}
if (A[x - 1][y] == 1) {
this.recursive(A, path, x - 1, y, row, col);
}
if (A[x + 1][y] == 1) {
this.recursive(A, path, x + 1, y, row, col);
}
if (A[x][y + 1] == 1) {
this.recursive(A, path, x, y + 1, row, col);
}
if (A[x][y - 1] == 1) {
this.recursive(A, path, x, y - 1, row, col);
}
}
}
class Solution1 {
int iLen = 0, jLen = 0;
public int numEnclaves(int[][] A) {
iLen = A.length;
jLen = A[0].length;
for (int i = 0; i < iLen; ++i) {
flood(i, 0, A);
flood(i, jLen - 1, A);
}
for (int j = 0; j < jLen; ++j) {
flood(0, j, A);
flood(iLen - 1, j, A);
}
int count = 0;
for (int i = 0; i < iLen; ++i)
for (int j = 0; j < jLen; ++j)
count += A[i][j];
return count;
}
private void flood(int i, int j, int[][] A) {
if (i == -1 || i == iLen || j == -1 || j == jLen || A[i][j] == 0)
return;
A[i][j] = 0;
flood(i - 1, j, A);
flood(i + 1, j, A);
flood(i, j - 1, A);
flood(i, j + 1, A);
}
}