Skip to main content

Connected Cells in a grid [MEDIUM] Java Solution














Question:


Consider a matrix with rows and columns, where each cell contains either a or a and any cell containing a is called a filled cell. Two cells are said to be connected if they are adjacent to each other horizontally, vertically, or diagonally; in other words, cell is connected to cells , , , , , , , and , provided that the location exists in the matrix for that .

If one or more filled cells are also connected, they form a region. Note that each cell in a region is connected to zero or more cells in the region but is not necessarily directly connected to all the other cells in the region.

Task
Given an matrix, find and print the number of cells in the largest region in the matrix. Note that there may be more than one region in the matrix.


Input Format


The first line contains an integer, , denoting the number of rows in the matrix.
The second line contains an integer, , denoting the number of columns in the matrix.
Each line of the subsequent lines contains space-separated integers describing the respective values filling each row in the matrix.


Constraints




Output Format


Print the number of cells in the largest region in the given matrix.


Sample Input
4 4 1 1 0 0 0 1 1 0 0 0 1 0 1 0 0 0



Sample Output
5


Explanation
The diagram below depicts two regions of the matrix; for each region, the component cells forming the region are marked with an X:
X X 0 0     1 1 0 0
0 X X 0     0 1 1 0
0 0 X 0     0 0 1 0
1 0 0 0     X 0 0 0
The first region has five cells and the second region has one cell. Because we want to print the number of cells in the largest region of the matrix, we print .







CODE:

import java.io.*;

import java.util.*;

import java.text.*;

import java.math.*;

import java.util.regex.*;




public class Solution {




public static void main(String[] args) {



Scanner sc = new Scanner(System.in);

int row =sc.nextInt();

int col = sc.nextInt();

int [][] arr= new int [row][col];

for(int i=0;i<row;i++){

for(int j=0;j<col;j++){

arr[i][j]=sc.nextInt();

}

}

System.out.println(getBiggestRegion(arr));

}

public static int getBiggestRegion(int [][] matrix){

int maxRegion=0;

for(int row=0;row<matrix.length;row++){

for(int col=0;col<matrix[row].length;col++){

if(matrix[row][col]==1){

int size = getRegion(matrix,row,col);

maxRegion = Math.max(size,maxRegion);

}

}

}

return maxRegion;

}

public static int getRegion(int [][] matrix, int row, int col){

if(row<0||col<0||row>=matrix.length||col>=matrix[row].length){

return 0;

}

if (matrix[row][col]==0){

return 0;

}

matrix[row][col]=0;

int size=1;

for (int r=row-1;r<=row+1;r++){ // look for the adjacent neighbors

for(int c=col-1;c<=col+1;c++){

if(r!=row||c!=col){

size+=getRegion(matrix,r,c);

}

}

}

return size;

}

}








Comments

Popular Posts

TARUNKUMAR RAWAT:Fourier Series and Fourier Transform

Tarunkumar Rawat Fourier Series: Click here to download Tarun Rawat Fourier Series Tarunkumar Rawat Fourier Transform: Click here to download TarunRawat Fourier Transform Steps to follow: 1) Click on the above link 2) Wait for 3 secs and click on the link 3) Wait for 5 secs and click on skip ad NOTE: We do require these ads to fund the site and provide books for free. Besides these are harmless and just require 5 secs waiting and nothing else. Please be patient for 5 secs and get your desired book for FREE!  IMPORTANT: We believe that every author deserves some respect and the same should be shown towards them by purchasing the books and lauding the efforts of author. Hence we recommend to buy the book. You can buy the book for an affordable rate in given below link: Using of PDF will be at your own risk and EXTC RESOURCES IS NOT RESPONSIBLE for any consequences of the same. We provide links for book and donot endorse piracy.  

Modern Digital and Analog Communication (BP Lathi,3rd ed)

Another Analog communication book: Modern Digital and Analog Communication (BP Lathi,3rd ed) : Click here to download Modern Digital and Analog Communication (BP Lathi,3rd ed) Copyright Disclaimer: This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.

Numerical methods with MATLAB

For Numerical methods along with MATLAB the best book is: Chapra Applied Numerical Methods MATLAB Engineers Scientists 3rd edition : Click here to download Steven Chapra with MATLAB For altenate link : Click here to download Steen Chapra with MATLAB