-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJuly_7.cpp
50 lines (44 loc) · 1010 Bytes
/
July_7.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
class Solution {
public:
int island(vector<vector<int>>&g,int i,int j)
{
int p=0;
if(i-1 < 0)
p++;
else
if(g[i-1][j] == 0)
p++;
if(i+1 >= g.size())
p++;
else
if(g[i+1][j] == 0)
p++;
if(j+1 >= g[i].size())
p++;
else
if(g[i][j+1] == 0)
p++;
if(j-1 < 0)
p++;
else
if(g[i][j-1] == 0)
p++;
return p;
}
int islandPerimeter(vector<vector<int>>& grid) {
int k, n=0;
for(int i=0; i<grid.size(); i++)
{
k=0;
for(int j=0; j<grid[i].size(); j++)
{
if(grid[i][j] == 1)
{
k = island(grid,i,j);
n = n + k;
}
}
}
return n;
}
};