-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathShortestBridge.java
79 lines (73 loc) · 2.53 KB
/
ShortestBridge.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
/*https://leetcode.com/problems/shortest-bridge/*/
class Solution {
public int shortestBridge(int[][] grid) {
int n = grid.length;
int firstX = -1, firstY = -1;
for (int i = 0; i < n; ++i)
{
for (int j = 0; j < n; ++j)
{
if (grid[i][j] == 1)
{
firstX = i;
firstY = j;
break;
}
}
}
List<int[]> landQueue = new ArrayList<>();
List<int[]> waterQueue = new ArrayList<>();
landQueue.add(new int[]{firstX, firstY});
waterQueue.add(new int[]{firstX, firstY});
grid[firstX][firstY] = 2;
while (!landQueue.isEmpty())
{
List<int[]> newLandQueue = new ArrayList<>();
for (int[] cell : landQueue)
{
int x = cell[0];
int y = cell[1];
for (int[] next : new int[][]{{x+1,y},{x-1,y},{x,y+1},{x,y-1}})
{
int currX = next[0];
int currY = next[1];
if (currX >= 0 && currX < n && currY >= 0 && currY < n && grid[currX][currY] == 1)
{
newLandQueue.add(new int[]{currX, currY});
waterQueue.add(new int[]{currX, currY});
grid[currX][currY] = 2;
}
}
}
landQueue = newLandQueue;
}
int distance = 0;
while (!waterQueue.isEmpty())
{
List<int[]> newWaterQueue = new ArrayList<>();
for (int[] cell : waterQueue)
{
int x = cell[0];
int y = cell[1];
for (int[] next : new int[][]{{x+1,y},{x-1,y},{x,y+1},{x,y-1}})
{
int currX = next[0];
int currY = next[1];
if (currX >= 0 && currX < n && currY >= 0 && currY < n)
{
if (grid[currX][currY] == 1)
return distance;
else if (grid[currX][currY] == 0)
{
newWaterQueue.add(new int[]{currX, currY});
grid[currX][currY] = -1;
}
}
}
}
waterQueue = newWaterQueue;
++distance;
}
return distance;
}
}