Source : https://www.hackerrank.com/challenges/connected-cell-in-a-grid
Consider a matrix where each cell contains either a or a . 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 the following grid, all cells marked X
are connected to the cell marked Y
.
XXX
XYX
XXX
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.
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.
For example, there are two regions in the following matrix. The larger region at the top left contains cells. The smaller one at the bottom right contains .
110100001
Function Description
Complete the connectedCell function in the editor below. It should return an integer that denotes the area of the largest region.
connectedCell has the following parameter(s):
- matrix: a 2D array of integers where represents the row of the matrix
Input Format
The first line contains an integer , the number of rows in the matrix.
The second line contains an integer , the number of columns in the matrix.
Each of the next lines contains space-separated integers .
Constraints
Output Format
Print the number of cells in the largest region in the given matrix.
Sample Input
441 1 0 00 1 1 00 0 1 01 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 00 X X 0 0 1 1 00 0 X 0 0 0 1 01 0 0 0 X 0 0 0
The first region has five cells and the second region has one cell. We print the size of the largest region.
Source : https://www.hackerrank.com/challenges/connected-cell-in-a-grid
Solution
// Karthikalapati.blogspot.com | |
import java.util.Scanner; | |
import java.util.ArrayList; | |
// Tips: | |
// - Instead of using a "boolean[][] visited" array, we alter our original grid | |
// - Dont create a 2-D "Point" or "Cell" class. It's not necessary. | |
public class Solution { | |
private static int rows; // here for convenience | |
private static int cols; // here for convenience | |
public static void main(String[] args) { | |
/* Save input grid */ | |
Scanner scan = new Scanner(System.in); | |
rows = scan.nextInt(); | |
cols = scan.nextInt(); | |
int [][] grid = new int[rows][cols]; | |
for (int row = 0; row < rows; row++) { | |
for (int col = 0; col < cols; col++) { | |
grid[row][col] = scan.nextInt(); | |
} | |
} | |
scan.close(); | |
System.out.println(largestRegion(grid)); | |
} | |
/* Returns the size of the largest region */ | |
public static int largestRegion(int [][] grid) { | |
int maxRegion = 0; | |
for (int row = 0; row < rows; row++) { | |
for (int col = 0; col < cols; col++) { | |
/* Find the largest region from the current cell */ | |
if (grid[row][col] == 1) { | |
int size = findLargestRegion(grid, row, col); | |
maxRegion = Math.max(maxRegion, size); | |
} | |
} | |
} | |
return maxRegion; | |
} | |
private static int findLargestRegion(int [][] grid, int row, int col) { | |
/* Put boundary checks here (at top of recursive call), instead of before doing recursive call */ | |
if (row < 0 || row >= rows || col < 0 || col >= cols || grid == null || grid[row][col] == 0) { | |
return 0; | |
} | |
grid[row][col] = 0; // we alter the original matrix here | |
int size = 1; // 1 accounts for our size | |
/* Recursively search neighbors */ | |
for (int r = row - 1; r <= row + 1; r++) { | |
for (int c = col - 1; c <= col + 1; c++) { | |
size += findLargestRegion(grid, r, c); | |
} | |
} | |
return size; | |
} | |
} |
No comments:
Post a Comment