-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path463.cpp
37 lines (35 loc) · 1.14 KB
/
463.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
class Solution {
public:
int islandPerimeter(vector<vector<int>> &grid) {
auto size = grid.size();
vector<vector<int>> visited;
for (int i = 0; i < size; ++i)
visited.push_back(vector<int>(grid[i].size(), 0));
for (int i = 0; i < size; ++i)
for (int j = 0; j < grid[i].size(); ++j)
if (grid[i][j]) {
helper(i, j, visited, grid);
break;
}
return num;
}
void helper(int &i, int &j, vector<vector<int>> &visited, vector<vector<int>> &grid) {
if (visited[i][j] == 1)
return;
visited[i][j] = 1;
int surrounded = 0;
for (int a = 0; a < 4; ++a) {
int tempX = i + dirx[a];
int tempY = j + diry[a];
if (tempX >= 0 && tempX < grid.size() && tempY >= 0 && tempY < grid[i].size() && grid[tempX][tempY] == 1) {
++surrounded;
helper(tempX, tempY, visited, grid);
}
}
num += (4 - surrounded);
}
private:
int num = 0;
int dirx[4] = {-1, 0, 0, 1};
int diry[4] = {0, 1, -1, 0};
};