#K53902. Largest Single Fruit Subgrid
Largest Single Fruit Subgrid
Largest Single Fruit Subgrid
You are given an orchard represented as an n × m grid, where each cell contains a character representing the type of fruit tree planted at that position.
Your task is to find the largest rectangular subgrid (of size at least 2×2) in which all trees are of the same type. The answer should be given as a list of four integers: [L, W, r, c]
, where:
L
is the number of rows in the subgrid,W
is the number of columns in the subgrid,r
andc
are the 0-indexed row and column of the top-left cell of the subgrid.
If no valid subgrid exists, output [0, 0, 0, 0]
.
The area of the subgrid is given by the formula: $$L \times W$$. You must choose the subgrid with the maximum possible area. In the case of multiple valid subgrids with the same area, choose the one that appears first based on the scanning order (top-to-bottom, left-to-right).
inputFormat
The first line of input contains two integers n and m (the number of rows and columns of the grid).
Each of the next n lines contains m characters separated by spaces, representing the orchard grid.
outputFormat
Output four space-separated integers: L W r c
representing the dimensions of the found subgrid and the starting row and column. If no valid subgrid exists, output 0 0 0 0
.
3 4
a a a b
a a a b
c c c c
2 3 0 0