Check if cells numbered 1 to K in a grid can be connected after removal of atmost one blocked cell


In this problem, we will check whether we can connect all K cells by unblocking any single cell.

To solve the problem, we will assume all connected K as a single Island. If any single blocked cell can connect all Island of the matrix, it is only possible to connect all K cells of the matrix. So, If the matrix contains more than 4 Island, it is not possible to connect all cells.

Problem Statement

We have given a mat[] matrix of size n*m. We have also given a positive integer, K. The matrix contains 0 and -1 in some cells. Also, the K cells of the matrix contain values from 1 to K. Here, 0 represents the unblocked cell, and -1 represents the blocked cell. We need to check whether we can connect all K cells by unblocking one blocked cell.

Note − We can connect cells only in horizontal and vertical directions.

Sample Examples

Input

mat = {{0, 2, 5, 0}, K = 6, N = 4, M = 4
      {-1, 6, -1, 3},
      {-1, -1, 4, -1},
      {0, -1, 1, -1}};

Output

Yes

Explanation

If we unblock the mat[1][2] cell, we can connect all K cells. We can connect any two cells via a cell containing 0, as it is an unblocked cell.

Input

mat = {{1, -1, 2, 0}, K = 4, N = 4, M = 4
      {-1, -1, -1, 0},
      {-1, -1, 3, -1},
      {4, -1, 0, -1}};

Output

No

Explanation

Here, we have 4 islands, and we can't connect them by unblocking any single cell.

Approach

In this approach, we will traverse through each cell. We will give all cells containing value 1 to K one number, representing from which Island it belongs.

After that, we will check for each blocked cell to see whether any blocked cell can connect all islands of the matrix. If possible, we can connect all cells of the matrix by unblocking the single cell.

Algorithm

  • Step 1 − Define the cells[k][2] matrix to store the all cells index containing values 1 to K, vis[][] matrix to keep track of the current cell visited, and 'cnt' with 0.

  • Step 2 − Traverse each cell of the matrix. If the current cell value is not 0 or -1, insert the p and q values at the 'cnt' index in the cells[][] matrix and increment the 'cnt' by 1. Also, updated vis[p][q] with false.

  • Step 3 − Define the dx[] and dy[] array and store all four horizontal and vertical directions. Also, initialize the 'sets' with 0.

  • Step 4 − Start traversing the cell[][] matrix.

  • Step 4.1 − Take the row and col value from the pth index, and if the row and col cell is already visited, move to the next iteration.

  • Step 4.2 − Increment the sets by 1 and define the queue to perform the BFS.

  • Step 4.3 − Insert the row and col pair in the queue.

  • Step 4.4 − Traverse while the queue is not empty.

  • Step 4.4.1 − Pop the first pair from the queue, and update the cell value with the 'set' value.

  • Step 4.4.2 − Traverse in all four directions. Also, check boundary conditions, and if the cell is already visited or the value is -1, move to the next iteration. Otherwise, mark current pairs as visited in the vis[][] matrix, and insert them into the queue.

  • Step 5 − Now, initialize the maxi with 0 to store a number of maximum sets any single blocking cell can connect.

  • Step 6 − Start traversing the matrix, and if the current cell value is -1, follow the below steps.

  • Step 6.1 − Define the set to store a number of islands that can be connected by the current cell.

  • Step 6.2 − Traverse in all four directions and store all unique cell values except 0 and -1 in the set. Here, the cell value is a value to which Island it belongs.

  • Step 6.3 − Take the set size and update the maxi if the set size is greater than the maxi.

  • Step 7 − Print' Yes' if maxi is equal to sets. Otherwise, print 'No'.

Example

#include <bits/stdc++.h>
using namespace std;
#define pairs pair<int, int>

void connectable(int k, vector<vector<int>> mat, int n, int m) {
   int cells[k][2];
   bool vis[n][m];
   int cnt = 0;
   // Cells matrix initialization
   for (int p = 0; p < n; p++) {
      for (int q = 0; q < m; q++) {
         if (mat[p][q] != 0 && mat[p][q] != -1) {
            cells[cnt][0] = p;
            cells[cnt][1] = q;
            cnt++;
         }
         vis[p][q] = false;
      }
   }
   // Directions
   int dx[] = {0, 0, 1, -1};
   int dy[] = {1, -1, 0, 0};
   // To store the total number of different sets
   int sets = 0;
   // BFS algorithm
   for (int p = 0; p < k; p++) {
      int row = cells[p][0], col = cells[p][1];
      if (vis[row][col])
         continue;
      sets++;
      queue<pairs> que;
      que.push(make_pair(row, col));
      vis[row][col] = true;
      while (!que.empty()) {
         pairs temp = que.front();
         que.pop();
         mat[temp.first][temp.second] = sets;
         // Moving in each four direction
         for (int q = 0; q < 4; q++) {
            // Visiting neighbor cells
            int temp_x = temp.first + dx[q];
            int temp_y = temp.second + dy[q];
            if (temp_x < 0 || temp_x >= n || temp_y < 0 || temp_y >= m)
               continue;
            if (vis[temp_x][temp_y] || mat[temp_x][temp_y] == -1)
               continue;
            que.push(make_pair(temp_x, temp_y));
            vis[temp_x][temp_y] = true;
         }
      }
   }
   int maxi = 0;
   for (int p = 0; p < n; p++) {
      for (int q = 0; q < m; q++) {
         // For blocked cell
         if (mat[p][q] == -1) {
            unordered_set<int> set;
            for (int r = 0; r < 4; r++) {
               int temp_x = p + dx[r];
               int temp_y = q + dy[r];
               if (temp_x < 0 || temp_x >= n || temp_y < 0 || temp_y >= m)
                  continue;
               // When an element is not from any set
               if (mat[temp_x][temp_y] <= 0)
                  continue;
               set.insert(mat[temp_x][temp_y]);
            }
            int s = set.size();
            maxi = max(s, maxi);
         }
      }
   }
   if (maxi == sets) {
      cout << "Yes, It is possible to connect all cells after removing one block!";
   } else {
      cout << "No, It is not possible to connect all cells of matrix!";
   }
}
int main() {
   int k = 6;
   int n = 4, m = 4;
   vector<vector<int>> mat = {{0, 2, 5, 0},
                              {-1, 6, -1, 3},
                              {-1, -1, 4, -1},
                              {0, -1, 1, -1}};
   connectable(k, mat, n, m);
   return 0;
}

Output

Yes, It is possible to connect all cells after removing one block!
  • Time complexity − O(M*N)

  • Space complexity − O(M*N)

Conclusion

We have given a unique set number to each cell of the matrix so that in the next step, we can check how many unique Islands can be connected by a particular blocked cell of the matrix.

Updated on: 25-Aug-2023

34 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements