| 1 | //This can be solved using DFS. |
| 2 | |
| 3 | int 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; |
no outgoing calls
no test coverage detected