COVID19 | Practice | GeeksforGeeks

Given the N*M binary matrix, 1 represents the healthy person, and 0 represents a patient affected by a coronavirus. The task is to check the minimum time required for all persons to get affected. A patient at [i, j] cell affects a person at cell [i, j-1], [i, j+1] [i+1, j] and [i-1, j] in one second. 
Note: There will be at least one patient 
Input: 
1. The first line of the input contains a single integer  T denoting the number of test cases. The description of T test cases follows.
2. The first line of each test case contains two space-separated integers N and M.
3. Next N lines contain M space-separated binary integers. 

Output: For each test case, print the minimum time required to all persons affected by COVID19

Constraints:
1. 1 <= T <= 100
2. 1 <= N, M <= 100
3. 0 <= mat[i][j] <= 1


Example:
Input:
2
2 2
1 0
1 0
3 3
1 1 1
1 0 1
1 1 1
Output:
1
2

Explanation:
Test Case 2: After first second matrix will look like {{1, 0, 1}, {0, 0, 0}, {1, 0, 1}}. After two seconds matrix will look like {{0, 0, 0}, {0, 0, 0}, {0, 0, 0}}.
+1
manojkumar8534533 months ago
Anyone know how dijsktra's algorithm is useful for this problem?
 
+1
amanpandey30074 months ago
#include <bits/stdc++.h>using namespace std;
int solve(vector<vector<int>>&grid,int n,int m){   queue<pair<int,int>>q;   vector<vector<int>>vis(n,vector<int>(m,0));   int count=0;   for(int i=0;i<n;i++)   {       for(int j=0;j<m;j++)       {           if(grid[i][j]==0)           {               q.push({i,j});               vis[i][j]=1;                          }           else           {               count++;           }       }   }           int day=0;   while(!q.empty())   {          if(count==0)               break;              day++;               int k=q.size();             while(k--)       {           auto p=q.front();           q.pop();                      int x=p.first;           int y=p.second;                      int dx[]={1,-1,0,0};           int dy[]={0,0,1,-1};                      for(int i=0;i<4;i++)           {               int nx=x+dx[i];               int ny=y+dy[i];                              if(nx<0 or ny<0 or nx>=n or ny>=m or vis[nx][ny])               continue;                              if(grid[nx][ny]==1)               {                      count--;                   q.push({nx,ny});                   vis[nx][ny]=1;               }                          }       }          }   return day;}int main() {   int t;   cin>>t;while(t--){   int n,m;   cin>>n>>m;   vector<vector<int>>grid(n,vector<int>(m,0));   for(int i=0;i<n;i++)   {       for(int j=0;j<m;j++)       {           cin>>grid[i][j];       }   }     cout<<solve(grid,n,m)<<endl;}return 0;}
0
Himanshu Shekher8 months ago
Himanshu Shekher
https://uploads.disquscdn.c...
0
Himanshu Shekher
This comment was deleted.
0
Himanshu Shekher
This comment was deleted.
0
tushar patni9 months ago
tushar patni
#include<iostream>#include<bits stdc++.h="">using namespace std;int isvalid(int i,int j,int n,int m,vector<vector<int>>&grid){    if(i>=n||j>=m||i<0||j<0||grid[i][j]!=1)    return 0;    return 1;}int ans(vector<vector<int>>&grid){        queue<pair<int,int>>q;        int c=0,n=grid.size(),m=grid[0].size();        for(int i=0;i<n;i++) for(int="" j="0;j&lt;m;j++)" if(grid[i][j]="=0)" q.push({i,j});="" q.push({-1,-1});="" while(!q.empty())="" {="" while(q.front().first!="-1&amp;&amp;q.front().second!=-1)" {="" int="" x="q.front().first;" int="" y="q.front().second;" q.pop();="" if(isvalid(x,y-1,n,m,grid))="" {="" grid[x][y-1]="0;" q.push({x,y-1});="" }="" if(isvalid(x,y+1,n,m,grid))="" {="" grid[x][y+1]="0;" q.push({x,y+1});="" }="" if(isvalid(x-1,y,n,m,grid))="" {="" grid[x-1][y]="0;" q.push({x-1,y});="" }="" if(isvalid(x+1,y,n,m,grid))="" {="" grid[x+1][y]="0;" q.push({x+1,y});="" }="" }="" c++;="" q.pop();="" if(q.empty())="" break;="" q.push({-1,-1});="" }="" for(int="" i="0;i&lt;n;i++)" for(int="" j="0;j&lt;m;j++)" if(grid[i][j]="=1)" return="" -1;="" return="" c-1;="" }="" int="" main()="" {="" int="" t;="" cin="">>t;     while(t--)     {         int n,m;         cin>>n>>m;         vector<vector<int>>grid(n,vector<int>(m,0));         for(int i=0;i<n;i++) for(int="" j="0;j&lt;m;j++)" cin="">>grid[i][j];         cout<<ans(grid)<<endl; }="" code="" return="" 0;="" }="">
+1
kartikeya Sharma9 months ago
kartikeya Sharma
#include <bits stdc++.h="">using namespace std;int main() {    int t;    cin >> t;    while(t--)    {        int n  , m;        cin >> n >> m;        int a[n][m];        pair<int ,="" int=""> p[4] = { {0 , 1} , {0, -1} , {1 , 0} , {-1 , 0} };                queue<pair<int ,="" int="">> q;        for(int i = 0 ; i < n ; i++)        {            for(int j = 0 ; j < m; j++ )            {                cin >> a[i][j];                if(a[i][j] == 0) q.push({i , j});            }        }                int ans = -1;        while(!q.empty())        {            int r = q.size();            for(int j = 0 ; j < r ; j++)            {                int x = q.front().first;                int y = q.front().second;                q.pop();                for(int i = 0 ; i < 4 ; i++)                {                    if(x-p[i].first>=0 && x-p[i].first <n &&="" y-p[i].second="">=0 && y-p[i].second<m) {="" if(a[x-p[i].first][y-p[i].second]="=1)" {="" q.push({x-p[i].first="" ,="" y-p[i].second});="" a[x-p[i].first][y-p[i].second]="0;" }="" }="" }="" }="" ans++;="" }="" cout="" <<="" ans="" <<="" endl;="" }="" }="" <="" code="">
+1
Suhaas Badada9 months ago
Suhaas Badada
https://pastebin.com/LhruHQwV
0
Imran Wahid9 months ago
Imran Wahid
Easy C++ solutionhttps://ide.geeksforgeeks.o...
0
62_YASH GOSWAMI10 months ago
62_YASH GOSWAMI
class GFG {  public static class pair{        int x;        int y;        pair(int x,int y)        {            this.x=x;            this.y=y;        }    }public static void main (String[] args) throws IOException {    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));    //no. of testcases    int t=Integer.parseInt(br.readLine());    while(t-->0)    {        String input[]=br.readLine().trim().split(" ");        // dimension of arr n*m             int n=Integer.parseInt(input[0]);        int m=Integer.parseInt(input[1]);        // arr declaration        int[][] arr=new int[n][m];        // arr input        for(int i=0;i<n;i++) {="" input="br.readLine().trim().split("" ");="" for(int="" j="0;j&lt;m;j++)" {="" arr[i][j]="Integer.parseInt(input[j]);" }="" }="" queue<pair=""> queue=new LinkedList<>();        for(int i=0;i<n;i++) for(int="" j="0;j&lt;m;j++)" {="" if(arr[i][j]="=0)" {="" queue.add(new="" pair(i,j));="" }="" }="" int[][]="" move="{{0,1},{1,0},{0,-1},{-1,0}};" int="" count="0;" while(!queue.isempty())="" {="" int="" size="queue.size();" while(size--="">0)           {    pair temp=queue.remove();               for(int i=0;i<4;i++)               {                   int x=temp.x+move[i][0];                   int y=temp.y+move[i][1];                   if(x>=0 && y>=0 && x<n &&="" y<m)="" {="" if(arr[x][y]="=1)" {="" arr[x][y]="0;" queue.add(new="" pair(x,y));="" }="" }="" }="" }="" count++;="" }="" system.out.println(count-1);="" }="" }="" }<="" code="">
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.