描述
給一個01矩陣,求不同的島嶼的個數(shù)烧给。
0代表海,1代表島喝噪,如果兩個1相鄰础嫡,那么這兩個1屬于同一個島。我們只考慮上下左右為相鄰酝惧。
樣例
在矩陣
[
[1, 1, 0, 0, 0],
[0, 1, 0, 0, 1],
[0, 0, 0, 1, 1],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 1]
]
有3個島
代碼
- BFS
class Coordinate {
int x, y;
public Coordinate(int x, int y) {
this.x = x;
this.y = y;
}
}
public class Solution {
/**
* @param grid a boolean 2D matrix
* @return an integer
*/
public int numIslands(boolean[][] grid) {
if (grid == null || grid.length == 0 || grid[0].length == 0) {
return 0;
}
int m = grid.length;
int n = grid[0].length;
int islands = 0;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (grid[i][j]) {
// 當點是1時才執(zhí)行markedByBST,所以markedByBST算法具體實現(xiàn)時不需要再判斷grid[i][j]對應的布爾值
markedByBST(grid, i, j);
islands++;
}
}
}
return islands;
}
// 用BFS尋找1點周圍四個點榴鼎,然后將找到的1點標記false
private void markedByBST(boolean[][] grid, int x, int y) {
int[] detalX = {0, 0, 1, -1};
int[] detalY = {1, -1, 0, 0};
Queue<Coordinate> queue = new LinkedList<>();
queue.offer(new Coordinate(x, y));
grid[x][y] = false;
while (!queue.isEmpty()) {
Coordinate coor = queue.poll();
for (int i = 0; i < 4; i++) {
Coordinate head = new Coordinate(coor.x + detalX[i], coor.y + detalY[i]);
if (!inBound(grid, head.x, head.y)) {
continue;
}
if (grid[head.x][head.y]) {
grid[head.x][head.y] = false;
queue.offer(head);
}
}
}
}
private boolean inBound(boolean[][] grid, int x, int y) {
int m = grid.length;
int n = grid[0].length;
return x >= 0 && x < m && y >= 0 && y < n;
}
}
- Union Find
class UnionFind {
private int[] father = null;
private int count;
private int find(int x) {
if (father[x] == x) {
return x;
}
return father[x] = find(father[x]);
}
public UnionFind(int n) {
// initialize your data structure here.
father = new int[n];
for (int i = 0; i < n; ++i) {
father[i] = i;
}
}
public void connect(int a, int b) {
int root_a = find(a);
int root_b = find(b);
if (root_a != root_b) {
father[root_a] = root_b;
count --;
}
}
public int query() {
return count;
}
public void set_count(int total) {
count = total;
}
}
public class Solution {
/**
* @param grid a boolean 2D matrix
* @return an integer
*/
public int numIslands(boolean[][] grid) {
int count = 0;
int n = grid.length;
if (n == 0)
return 0;
int m = grid[0].length;
if (m == 0)
return 0;
UnionFind union_find = new UnionFind(n * m);
int total = 0;
for(int i = 0;i < grid.length; ++i)
for(int j = 0;j < grid[0].length; ++j)
if (grid[i][j])
total ++;
union_find.set_count(total);
for(int i = 0;i < grid.length; ++i)
for(int j = 0;j < grid[0].length; ++j)
if (grid[i][j]) {
if (i > 0 && grid[i - 1][j]) {
union_find.connect(i * m + j, (i - 1) * m + j);
}
if (i < n - 1 && grid[i + 1][j]) {
union_find.connect(i * m + j, (i + 1) * m + j);
}
if (j > 0 && grid[i][j - 1]) {
union_find.connect(i * m + j, i * m + j - 1);
}
if (j < m - 1 && grid[i][j + 1]) {
union_find.connect(i * m + j, i * m + j + 1);
}
}
return union_find.query();
}
}
- DFS (not recommended)
public class Solution {
/**
* @param grid a boolean 2D matrix
* @return an integer
*/
private int m, n;
public void dfs(boolean[][] grid, int i, int j) {
if (i < 0 || i >= m || j < 0 || j >= n) return;
if (grid[i][j]) {
grid[i][j] = false;
dfs(grid, i - 1, j);
dfs(grid, i + 1, j);
dfs(grid, i, j - 1);
dfs(grid, i, j + 1);
}
}
public int numIslands(boolean[][] grid) {
// Write your code here
m = grid.length;
if (m == 0) return 0;
n = grid[0].length;
if (n == 0) return 0;
int ans = 0;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (!grid[i][j]) continue;
ans++;
dfs(grid, i, j);
}
}
return ans;
}
}