-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path200. Number of Islands.cpp
74 lines (57 loc) · 1.7 KB
/
200. Number of Islands.cpp
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
/*
Link: https://leetcode.com/problems/number-of-islands/
Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.
Example 1:
Input:
11110
11010
11000
00000
Output: 1
Example 2:
Input:
11000
11000
00100
00011
Output: 3
*/
//Solution 1 Using DFS
class Solution {
public:
int numIslands(vector<vector<char>>& grid) {
int i, j, m, n;
m = grid.size();
if(!m)
return 0;
n = grid[0].size();
vector<vector<int>> visited(m, vector<int> (n, 0));
int count = 0;
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
if (grid[i][j] == '1' && !visited[i][j]) {
DFS (grid, visited, i, j);
count ++;
}
}
}
return count;
}
void DFS (vector<vector<char>>& grid, vector<vector<int>> &visited, int row, int col) {
int r[4] = {0, 0, -1, 1};
int c[4] = {-1, 1, 0, 0};
visited[row][col] = 1;
for (int i = 0; i < 4; i++) {
if (isTrue (grid, visited, row + r[i], col + c[i])) {
DFS (grid, visited, row + r[i], col + c[i]);
}
}
}
bool isTrue (vector<vector<char>>& grid, vector<vector<int>> &visited, int row, int col) {
return row >= 0 && col >= 0
&& row < grid.size()
&& col < grid[0].size()
&& !visited[row][col]
&& grid[row][col] == '1';
}
};