Making A Large Island in C++


Suppose we have a 2D grid of binary values (0s and 1s), we change at most one 0 to a 1. After that we have to find what is the size of the largest island? Here an island is a 4-directionally (top, bottom, left, right) connected group of 1s.

So, if the input is like [[1, 0], [0, 1]], then the output will be 3, this is because if we change one 0 to 1 and connect two 1s, then we will get an island with area = 3.

To solve this, we will follow these steps −

  • Define an array dir of size: 4 x 2, dir := {{1, 0}, { - 1, 0}, {0, 1}, {0, - 1}}

  • Define a function dfs(), this will take idx, i, j, the grid,

  • if (i,j) are inside the grid region and grid[i, j] is not equal to 1, then −

    • return 0

  • ret := 1

  • grid[i, j] := idx

  • for initialize k := 0, when k < 4, update (increase k by 1), do −

    • ni := dir[k, 0] + i, nj := dir[k, 1] + j

    • ret := ret + dfs(grid, ni, nj, idx)

  • return ret

  • From the main method, do the following −

  • ret := 0, idx := 2

  • Define an array area of size 2

  • n := Row count of grid, m := column count of grid

  • for initialize i := 0, when i < n, update (increase i by 1), do −

    • for initialize j := 0, when j < m, update (increase j by 1), do −

      • if grid[i, j] is same as 1, then −

        • insert dfs(grid, i, j, idx) at the end of area

        • ret := maximum of ret and last element of area

        • (increase idx by 1)

  • for initialize i := 0, when i < n, update (increase i by 1), do −

    • if grid[i, j] is same as 0, then −

      • Define one set idxs

      • for initialize k := 0, when k < 4, update (increase k by 1), do −

        • ni := i + dir[k, 0],nj := j + dir[k, 1]

        • if ni,nj in the range of grid, then −

          • if grid[ni, nj] is non-zero, then −

            • insert grid[ni, nj] into idxs

      • temp := 1

      • for all elements it in idxs, do −

        • temp := temp + area[it]

        • (increase it by 1)p + area[it]

    • ret := maximum of ret and temp

  • return ret

Let us see the following implementation to get better understanding −

Example

 Live Demo

#include <bits/stdc++.h>
using namespace std;
int dir[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
class Solution {
   public:
   int dfs(vector < vector <int> >& grid, int i, int j, int idx){
      if(i < 0 || j < 0 || i >= grid.size() || j >= grid[0].size()
      || grid[i][j] != 1) return 0;
      int ret = 1;
      grid[i][j] = idx;
      for(int k = 0; k < 4; k++){
         int ni = dir[k][0] + i;
         int nj = dir[k][1] + j;
         ret += dfs(grid, ni, nj, idx);
      }
      return ret;
   }
   int largestIsland(vector<vector<int>>& grid) {
      int ret = 0;
      int idx = 2;
      vector <int > area(2);
      int n = grid.size();
      int m = grid[0].size();
      for(int i = 0; i < n; i++){
         for(int j = 0; j < m; j++){
            if(grid[i][j] == 1){
               area.push_back(dfs(grid, i, j, idx));
               ret = max(ret, area.back());
               idx++;
            }
         }
      }
      for(int i = 0; i < n; i++){
         for(int j = 0; j < m; j++){
            if(grid[i][j] == 0){
               set <int> idxs;
               for(int k = 0; k < 4; k++){
                  int ni = i + dir[k][0];
                  int nj = j + dir[k][1];
                  if(ni < 0 || nj < 0 || ni >= grid.size() ||
                  nj >= grid[0].size()) continue;
                  if(grid[ni][nj]){
                     idxs.insert(grid[ni][nj]);
                  }
               }
               int temp = 1;
               set <int> :: iterator it = idxs.begin();
               while(it != idxs.end()){
                  temp += area[*it];
                  it++;
               }
               ret = max(ret, temp);
            }
         }
      }
      return ret;
   }
};
main(){
   Solution ob;
   vector<vector<int>> v = {{1,0},{0,1}};
   cout << (ob.largestIsland(v));
}

Input

{{1,0},{0,1}}

Output

3

Updated on: 08-Jun-2020

158 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements