forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
40 lines (38 loc) · 1.06 KB
/
Solution.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
class Solution {
private int[] p;
public int swimInWater(int[][] grid) {
int n = grid.length;
p = new int[n * n];
for (int i = 0; i < p.length; ++i) {
p[i] = i;
}
int[] hi = new int[n * n];
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
hi[grid[i][j]] = i * n + j;
}
}
int[] dirs = {-1, 0, 1, 0, -1};
for (int t = 0; t < n * n; ++t) {
int i = hi[t] / n;
int j = hi[t] % n;
for (int k = 0; k < 4; ++k) {
int x = i + dirs[k];
int y = j + dirs[k + 1];
if (x >= 0 && x < n && y >= 0 && y < n && grid[x][y] <= t) {
p[find(x * n + y)] = find(i * n + j);
}
if (find(0) == find(n * n - 1)) {
return t;
}
}
}
return -1;
}
private int find(int x) {
if (p[x] != x) {
p[x] = find(p[x]);
}
return p[x];
}
}