#C9256. Flower Bouquet Selection
Flower Bouquet Selection
Flower Bouquet Selection
You are given a rectangular garden represented as an n x m grid where each cell contains a flower denoted by an uppercase letter or a dot .
indicating an empty spot. Your task is to determine if you can pick a bouquet by selecting a contiguous subgrid (a rectangle) having an area equal to p (height \(\times\) width = p) so that all cells in that subgrid contain the same type of flower (and none is a dot).
For example, consider a subgrid of size 2 x 2 (i.e., 4 cells) and p = 4. If all cells have the letter A
, then you can form a bouquet. Otherwise, the bouquet cannot be picked.
Output YES
if such a subgrid exists; otherwise, output NO
.
inputFormat
The first line contains an integer t
representing the number of test cases. Each test case begins with a line containing three integers n
, m
, and p
, where n
and m
denote the number of rows and columns of the garden, respectively, and p
is the required bouquet area such that \(\text{height} \times \text{width} = p\) (in \(\LaTeX\) this is represented as \(\text{height} \times \text{width} = p\)
). Following this, there are n
lines, each containing a string of length m
that describes a row of the garden grid. A cell contains an uppercase letter (flower type) or a dot (.
) indicating an empty spot.
outputFormat
For each test case, print a single line containing YES
if it is possible to pick a bouquet by selecting a contiguous subgrid with area p
in which all cells contain the same flower (and are not dots), otherwise print NO
.
2
3 4 4
A..B
.A.B
AAAA
4 4 5
B.BB
BCDD
BCCD
BBBB
YES
NO
</p>