Contain Virus

Posted: 4 Apr, 2021
Difficulty: Hard


Try Problem

You have been given a 2D model of a country, with the help of a matrix containing 0 and 1. The 0’s in the model represents uninfected cells and 1’s represent the cells contaminated by a deadly spreading virus. Only a single wall can be built between any two cells of the model that are adjacent in all four directions, on their shared boundary.

With every passing night the virus spreads to all the adjacent cells, unless the cells are blocked by a wall. You need to build walls in order to stop the virus from spreading in the whole country. Note that you can only install walls around only one region which is the affected area that threatens to infect the most uninfected cells of the matrix in one day.

Given the state of each cell, your task is to find the number of walls you used to stop as many cells as possible from being infected.

Input Format:
The first line contains an integer ‘T’ which denotes the number of test cases or queries to be run. Then the test cases are as follows.

The first line of each test case contains two space-separated integers ‘N’ and ‘M’, denoting the dimensions of the country model.
Each of the next ‘N’ lines contain ‘M’ elements each denoting the state of the cell.
Output Format:
For each test case, print a single integer denoting the number of walls required.

Print the output of each test case in a separate line.
You don’t need to print anything; It has already been taken care of. Just implement the given function.
1 <= T <= 10
1 <= N, M <= 100
0 <= X <= 1

Where ‘T’ is the number of test cases, ‘N’ and ‘M’ denotes the dimensions of the model, ‘X’ denotes the element of the matrix.

Time limit: 1 sec
Approach 1

The approach is to get clusters of infected areas in each step. Deal with the largest cluster and expand the rest of the clusters.


Let us understand this using an example:



    1 1 1 0 0 0 0 0 0 

    1 0 1 0 1 1 1 1 1

    1 1 1 0 0 0 0 0 0

First iteration:

The highlighted area is the largest cluster as it infects the most cells. So, we will make walls around this area, in this case 11 walls and make the neighbors infected for the other infected area.


Second Iteration:

The highlighted area now is the largest cluster and the only cluster left in this case. So, make walls around the area, in this case 2 walls that touch the non-infected area.


Now, there are no more infected areas that can infect the cells. So, return the number of walls used,  in this case 11 + 2 = 13 walls.




  • To start with, we will create a class “Cluster”, consisting of a set of cells that are infected, a set of cells that are next to this cell and are not infected, and the count of walls required to contain this structure. This class will be used for all the clusters.
  • In the main function, create a visited matrix and initialise a variable “ans” = 0.
  • Now, search for the infected cell in the given model using DFS.
    • Create a cluster for every value and apply DFS.
    • For DFS, create a recursive function:
      • If the current cell is out of model bounds or if the cell is already visited or if the current cell is already blocked by a wall, then we will simply return.
      • If the cell is not infected then add it to the set of not infected cells and increment the wall count.
      • Otherwise, add it to the set of infected cells and mark it as visited.
      • Finally call DFS in all the directions of the infected cells.
  • Get the biggest cluster by finding the cluster with the maximum number of non-infected adjacent cells out of all the created clusters, Make all cells belonging to the biggest cluster as -1 and add the number of required walls to contain them, and add them to “ans”, the variable used to store the total number of walls required.
  • For the rest of the clusters make their not infected cells as infected.
  • Return ans.
Try Problem