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

Method dfs

LeetCode_problems/Number of Islands/solution1.cpp:5–19  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

3
4
5 void dfs(int i , int j, vector<vector<char>> &arr, vector<vector<int>> &vis){ //function for DFS
6
7 vis[i][j]=1; // mark island as visited
8
9 int dx[]={0,0,1,-1}; // array for changing values of x
10 int dy[]={-1,1,0,0}; // array for changing values of y
11
12 for(int k=0;k<4;k++){
13 if(i+dx[k]<arr.size() && i+dx[k]>=0 && j+dy[k]<arr[0].size() && j+dy[k]>=0 && arr[i+dx[k]][j+dy[k]]=='1' && vis[i+dx[k]][j+dy[k]]==0){ // if index (i+dx,j+dy) is in bounds of the grid, value at that index is 1 and index is not visited, mark it as visited and then perform DFS on it
14 vis[i+dx[k]][j+dy[k]]=1;
15 dfs(i+dx[k],j+dy[k],arr,vis);
16 }
17 }
18
19 }
20
21
22 int numIslands(vector<vector<char>>& grid) {

Callers

nothing calls this directly

Calls 1

dfsFunction · 0.50

Tested by

no test coverage detected