MCPcopy Create free account
hub / github.com/Vishruth-S/CompetitiveCode / dfs

Function dfs

LeetCode_problems/Max Area of Island/solution.cpp:3–19  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1//This can be solved using DFS.
2
3int dfs(int x,int y, vector<vector<int>>&grid, vector<vector<bool>>&visited)
4{// If a point is out of bounds or if is already visited then we return 0;
5 if(x<0 || y<0 || x>=grid.size()|| y>=grid[0].size() || visited[x][y] == true || grid[x][y] == 0){
6 return 0;
7 }
8 else
9 {
10 // Else we make the point visited and then search if its neighbours are 0 or 1 if they are 1 then the size of island is increased, else it is made 0;
11 v[x][y] = true;
12 // we do DFS here
13 int top = dfs(x-1,y,grid,visited);
14 int left = dfs(x,y-1,grid,visited);
15 int right = dfs(x,y+1,grid,visited);
16 int down = dfs(x+1,y,grid,visited);
17 return (1+(left+right+down+top));
18 }
19 }
20
21 int maxAreaOfIsland(vector<vector<int>>& grid) {
22 int ans = 0;

Callers 5

maxAreaOfIslandFunction · 0.70
dfsMethod · 0.50
numIslandsMethod · 0.50
dfsMethod · 0.50
uniquePathsIIIMethod · 0.50

Calls

no outgoing calls

Tested by

no test coverage detected