-
Notifications
You must be signed in to change notification settings - Fork 0
/
Solution.java
97 lines (79 loc) · 2.44 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
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
90
91
92
93
94
95
96
97
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Set;
class Pair {
int x;
int y;
public Pair(int x, int y) {
this.x = x;
this.y = y;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Pair)) return false;
Pair pair = (Pair) o;
if (x != pair.x) return false;
return y == pair.y;
}
@Override
public int hashCode() {
int result = x;
result = 31 * result + y;
return result;
}
}
public class Solution {
public int numIslands(char[][] grid) {
Set<Pair> visited = new HashSet<>();
int numberOfIsland = 0;
for (int xIndex = 0; xIndex < grid.length; xIndex++) {
for (int yIndex = 0; yIndex < grid[xIndex].length; yIndex++) {
// System.out.format("outter layer %d %d", xIndex, yIndex);
if(visited.contains(new Pair(xIndex, yIndex)) || grid[xIndex][yIndex] == '0') {
continue;
} else {
bfs(xIndex, yIndex, grid, visited);
numberOfIsland ++;
}
}
}
return numberOfIsland;
}
private void bfs(int x, int y, char[][] grid , Set<Pair> visited) {
Queue<Pair> front = new LinkedList<>();
front.add(new Pair(x, y));
int level = 0;
while(front.peek() != null){
Pair current = front.poll();
// System.out.println("processing >" + current.x + " - " + current.y);
if(visited.contains(current)) {
continue;
}
visited.add(current);
Queue<Pair> neighbours = getNeighbours(grid, current);
front.addAll(neighbours);
}
}
private Queue<Pair> getNeighbours( char[][] grid, Pair front) {
Queue<Pair> neighbours = new LinkedList<>();
int x =front.x;
int y = front.y;
if(grid[x][y] == '1') {
if(x < grid.length - 1 ) {
neighbours.add(new Pair(x + 1, y));
}
if(x > 0) {
neighbours.add(new Pair(x - 1, y));
}
if(y < grid[x].length- 1 ) {
neighbours.add(new Pair(x, y +1));
}
if(y !=0) {
neighbours.add(new Pair(x, y - 1));
}
}
return neighbours;
}
}