-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path200.h
48 lines (42 loc) · 1.06 KB
/
200.h
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
//
// Created by 17336 on 2022/3/6.
//
#ifndef HOT100_200_H
#define HOT100_200_H
#include <vector>
using namespace std;
class Solution {
int count = 0;
public:
int numIslands(vector<vector<char>> &grid) {
int res=0;
for (int i = 0; i < grid.size(); ++i) {
for (int j = 0; j < grid[i].size(); ++j) {
if(grid[i][j]=='1') {
++res;
dfs(grid,i,j);
}
}
}
return res;
}
void dfs(vector<vector<char>> &grid, int i, int j) {
if (i < grid.size() - 1 && grid[i + 1][j] == '1') {
grid[i + 1][j] = '2';
dfs(grid, i + 1, j);
}
if (j < grid[i].size() - 1 && grid[i][j + 1] == '1') {
grid[i][j + 1] = '2';
dfs(grid, i, j + 1);
}
if (i > 0 && grid[i-1][j]=='1'){
grid[i-1][j]='2';
dfs(grid,i-1,j);
}
if(j>0&&grid[i][j-1]=='1'){
grid[i][j-1]='2';
dfs(grid,i,j-1);
}
}
};
#endif //HOT100_200_H