-
Notifications
You must be signed in to change notification settings - Fork 46
/
IslandPerimeter.cpp
39 lines (33 loc) · 1.14 KB
/
IslandPerimeter.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
/*
The solution is basically based on DFS of a graph.
The Borders in the graph are shown as 1s so that they may add upto the perimeter.
*/
class Solution {
public:
int DFS(vector<vector<int>>& grid,int x, int y,vector<vector<bool>>&visited){
if(x<0||y<0||x>=grid.size()||y>=grid[0].size()||!grid[x][y]) // Border Condition Checking
return 1;
if(visited[x][y]) // Already Visited Graph
return 0;
visited[x][y] = true;
int a = DFS(grid,x+1,y,visited); // DFS Calls for Adjacent cells
a +=DFS(grid,x-1,y,visited);
a +=DFS(grid,x,y+1,visited);
a +=DFS(grid,x,y-1,visited);
return a;
}
int islandPerimeter(vector<vector<int>>& grid) {
vector<vector<bool>>visited(grid.size(),vector<bool>(grid[0].size(),false));
for(int i = 0;i<grid.size();i++)
{
for(int j = 0;j<grid[i].size();j++)
{
if(grid[i][j]) // Calls for all the connected Components
{
return DFS(grid,i,j,visited);
}
}
}
return 0;
}
};