-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathNumberOfIslands.java
More file actions
142 lines (129 loc) · 3.13 KB
/
NumberOfIslands.java
File metadata and controls
142 lines (129 loc) · 3.13 KB
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/**
*
*/
package cc.dectinc.leetcode;
import java.util.LinkedList;
import java.util.Queue;
/**
* @author chenshijiang
* @date Apr 21, 2015 3:47:11 PM
*
*/
public class NumberOfIslands {
private final char landChar = '1';
private final char waterChar = '0';
private int m, n;
private final int[][] bounds = { { 1, 0 }, { -1, 0 }, { 0, 1 }, { 0, -1 } };
public int numIslands(char[][] grid) {
m = grid.length;
if (m == 0) {
return 0;
}
n = grid[0].length;
int result = 0;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (grid[i][j] == landChar) {
result++;
bfs(grid, i, j);
}
}
}
return result;
}
private void bfs(char[][] grid, int x, int y) {
Queue<Integer> queue = new LinkedList<Integer>();
queue.add(x);
queue.add(y);
while (!queue.isEmpty()) {
x = queue.poll();
y = queue.poll();
for (int i = 0; i < 4; i++) {
int tmpX = x + bounds[i][0];
int tmpY = y + bounds[i][1];
if (tmpX >= 0 && tmpX < m && tmpY >= 0 && tmpY < n
&& grid[tmpX][tmpY] == landChar) {
grid[tmpX][tmpY] = waterChar;
queue.add(tmpX);
queue.add(tmpY);
}
}
}
}
private void bfsDoubleQueue(char[][] grid, int x, int y) {
Queue<Integer> abscissa = new LinkedList<Integer>();
Queue<Integer> ordinate = new LinkedList<Integer>();
abscissa.add(x);
ordinate.add(y);
while (!abscissa.isEmpty()) {
x = abscissa.poll();
y = ordinate.poll();
for (int i = 0; i < 4; i++) {
int tmpX = x + bounds[i][0];
int tmpY = y + bounds[i][1];
if (tmpX >= 0 && tmpX < m && tmpY >= 0 && tmpY < n
&& grid[tmpX][tmpY] == landChar) {
grid[tmpX][tmpY] = waterChar;
abscissa.add(tmpX);
ordinate.add(tmpY);
}
}
}
}
/**
* This method is not able to handle cases while there are circle islands.
*
* @param grid
* 2d grid map
* @return number of islands
*/
public int numIslandsFailed(char[][] grid) {
int m = grid.length;
if (m == 0) {
return 0;
}
int n = grid[0].length;
int result = grid[0][0] == landChar ? 1 : 0;
for (int i = 1; i < m; i++) {
if (grid[i][0] == landChar && grid[i - 1][0] == waterChar) {
result += 1;
}
}
for (int j = 1; j < n; j++) {
if (grid[0][j] == landChar && grid[0][j - 1] == waterChar) {
result += 1;
}
}
for (int i = 1; i < m; i++) {
for (int j = 1; j < n; j++) {
char c = grid[i][j];
if (c == landChar) {
if (grid[i - 1][j - 1] == waterChar
&& grid[i - 1][j] == landChar
&& grid[i][j - 1] == landChar) {
result--;
}
if (grid[i - 1][j] == waterChar
&& grid[i][j - 1] == waterChar) {
result++;
}
}
}
}
return result;
}
public static void main(String[] args) {
NumberOfIslands sol = new NumberOfIslands();
String[] aGrid = { "111", "101", "111" };
// String[] aGrid = { "11000", "11000", "00100", "00011" };
int m = aGrid.length;
int n = aGrid[0].length();
char[][] grid = new char[aGrid.length][aGrid[0].length()];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
grid[i][j] = aGrid[i].charAt(j);
}
}
System.out.println(sol.numIslands(grid));
}
}