Minimum Number of Days to Disconnect Island - Codeprg

Breaking

programing News Travel Computer Engineering Science Blogging Earning

Monday 31 August 2020

Minimum Number of Days to Disconnect Island

Minimum Number of Days to Disconnect Island


Minimum Number of Days to Disconnect Island

Given a 2D grid consisting of 1s (land) and 0s (water).  An island is a maximal 4-directionally (horizontal or vertical) connected group of 1s.

The grid is said to be connected if we have exactly one island, otherwise is said disconnected.

In one day, we are allowed to change any single land cell (1) into a water cell (0).

Return the minimum number of days to disconnect the grid.

 

Example 1:

Input: grid = [[0,1,1,0],[0,1,1,0],[0,0,0,0]]
Output: 2
Explanation: We need at least 2 days to get a disconnected grid.
Change land grid[1][1] and grid[0][2] to water and get 2 disconnected island.

Example 2:

Input: grid = [[1,1]]
Output: 2
Explanation: Grid of full water is also disconnected ([[1,1]] -> [[0,0]]), 0 islands.

Example 3:

Input: grid = [[1,0,1,0]]
Output: 0

Example 4:

Input: grid = [[1,1,0,1,1],
               [1,1,1,1,1],
               [1,1,0,1,1],
               [1,1,0,1,1]]
Output: 1

Example 5:

Input: grid = [[1,1,0,1,1],
               [1,1,1,1,1],
               [1,1,0,1,1],
               [1,1,1,1,1]]
Output: 2

 

Constraints:

  • 1 <= grid.length, grid[i].length <= 30
  • grid[i][j] is 0 or 1.

 class Solution {

public:

    void island_recur(vector<vector<int> > &g,int i, int j,int n,int m,vector<vector<bool> > & vis)

    {

         if(vis[i][j]||g[i][j]==0)return;

          vis[i][j]=true;

         if(i>0)island_recur(g,i-1,j,n,m,vis);

         if(j>0)island_recur(g,i,j-1,n,m,vis);

         if(i+1<n)island_recur(g,i+1,j,n,m,vis);

         if(j+1<m)island_recur(g,i,j+1,n,m,vis);

    }

    

    int no_island(vector< vector<int> > &g)

    {

        int ret=0;

        int n=g.size(),m=g[0].size();

         vector<vector<bool>> vis(n, vector<bool> (m, 0));

          for(int i=0;i<n;++i)

         {

            for(int j=0;j<m;++j)

            {

                 if(g[i][j] and !vis[i][j]) 

                 {

                    island_recur(g,i,j,n,m,vis);

                      ret+=1;

                 }    

            }

          }

        return ret;

    }

    int minDays(vector<vector<int>>& grid) {

        if(grid.size()==0)return 0;

        int time=0;

        int l=no_island(grid);

        if(l!=1)

            return time;

        time=1;

        

        int n=grid.size(),m=grid[0].size();

        for(int i=0;i<n;++i)

        {

            for(int j=0;j<m;++j)

            {

                   if(grid[i][j])

                   { 

                     grid[i][j]=0;

                     l=no_island(grid);

                     grid[i][j]=1;

                     if(l!=1)

                             return time;

                   }  

            }

        }

        return 2;

        

    }

};