-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathNumberOfIslands.java
117 lines (107 loc) · 3.23 KB
/
NumberOfIslands.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/*https://practice.geeksforgeeks.org/problems/find-the-number-of-islands/1*/
/*https://binarysearch.com/problems/Number-of-Islands*/
//using DFS
class Solution {
char[][] grid;
int r, c;
public int numIslands(char[][] grid) {
int count = 0;
this.grid = grid;
this.r = grid.length;
this.c = grid[0].length;
//check for each cell
for (int i = 0; i < r; ++i)
for (int j = 0; j < c; ++j)
if (this.grid[i][j] == '1') //if we are still left with unvisited cells
{
runDFS(i, j); //run DFS on it
++count; //increment the count
}
return count;
}
public void runDFS(int row, int col)
{
//if we consider the diagonals
int rows[] = new int[] { -1, -1, -1, 0, 0, 1, 1, 1 };
int cols[] = new int[] { -1, 0, 1, -1, 1, -1, 0, 1 };
//if we don't consider diagonals then remove the comment here
/*int rows[] = new int[] {-1, 0, 0, 1};
int cols[] = new int[] {0, -1, 1, 0};*/
// Mark this cell as visited
grid[row][col] = '0';
// Recur for all connected neighbours
for (int k = 0; k < 8; ++k) //replace 8 with 4 for the other case
if ((row + rows[k] >= 0) && (row + rows[k] < r) && (col + cols[k] >= 0) && (col + cols[k] < c) && (grid[row + rows[k]][col + cols[k]] == '1'))
runDFS(row + rows[k], col + cols[k]);
}
}
/*DFS*/
import java.util.*;
class Solution {
int m, n;
int[][] d = new int[][]{{1,0},{-1,0},{0,1},{0,-1}};
boolean[][] visited;
public int solve(int[][] matrix) {
m = matrix.length; n = matrix[0].length;
visited = new boolean[m][n];
int i, j, count = 0;
for (i = 0; i < m; ++i)
{
for (j = 0; j < n; ++j)
{
if (matrix[i][j] == 1 && !visited[i][j])
{
++count;
dfs(matrix, i, j);
}
}
}
return count;
}
public void dfs(int[][] mt, int row, int col)
{
visited[row][col] = true;
int i, r, c;
for (i = 0; i < 4; ++i)
{
r = row+d[i][0];
c = col+d[i][1];
if (r >= 0 && r < m && c >= 0 && c < n && mt[r][c] == 1 && !visited[r][c])
dfs(mt, r, c);
}
}
}
/*Without visited matrix*/
import java.util.*;
class Solution {
int m, n;
int[][] d = new int[][]{{1,0},{-1,0},{0,1},{0,-1}};
public int solve(int[][] matrix) {
m = matrix.length; n = matrix[0].length;
int i, j, count = 0;
for (i = 0; i < m; ++i)
{
for (j = 0; j < n; ++j)
{
if (matrix[i][j] == 1)
{
++count;
dfs(matrix, i, j);
}
}
}
return count;
}
public void dfs(int[][] mt, int row, int col)
{
mt[row][col] = 0;
int i, r, c;
for (i = 0; i < 4; ++i)
{
r = row+d[i][0];
c = col+d[i][1];
if (r >= 0 && r < m && c >= 0 && c < n && mt[r][c] == 1)
dfs(mt, r, c);
}
}
}