AAB BBA BBB
can be colored in four strokes as follows:
... ..B AAB AAB AAB ... -> ... -> ... -> BB. -> BBA ... ... ... BBB BBBIt is not possible to produce the end result using less than four strokes.
The first line contains N, M, and Q.
The next N lines each contain a string of M uppercase characters representing the desired colors for each row of the canvas.
The next Q lines each contain four space-separated integers x1,y1,x2,y2 representing a candidate subrectangle (1≤x1≤x2≤N, 1≤y1≤y2≤M).
For each of the Q candidates, output the answer on a new line.
4 8 9 ABBAAAAA ABAAAABA CAADABBA AAAAAAAA 1 1 4 8 3 5 3 8 1 3 2 4 1 4 2 5 1 1 3 3 4 4 4 4 2 6 4 8 3 5 4 6 1 6 3 8
The first candidate consists of the entire canvas, which can be painted in six strokes.
The second candidate consists of the subrectangle with desired colors
ABBA
and can be colored in three strokes. Note that although the cells at (3,5) and (3,8) can be colored with A in a single stroke if you consider the entire canvas, this is not the case when considering only the cells within the subrectangle.
Test cases 1-2 satisfy N,M≤50.
In test cases 3-5, the canvas contains no cycles of a single color. That is, there does not exist a sequence of distinct cells c1,c2,c3,…,ck such that all of the following conditions are satisfied:
·k>2
·All of c1,c2,c3,…,ck have the same desired color.
·ci is adjacent to ci+1 for each 1≤i<k.
·ck is adjacent to c1.
Note that the 3×3 canvas above contains a cycle of a single color (the four Bs in the bottom-left corner).
·In test cases 6-8, every connected component consisting of cells with the same desired color can be contained within a two by two square with sides parallel to the coordinate axes. The 3×3 canvas above does not satisfy this property (the connected component with five Bs cannot be contained within a two by two square).
·In test cases 9-11, every connected component consisting of cells with the same desired color can be contained within a three by three square with sides parallel to the coordinate axes. The 3×3 canvas above satisfies this property.
·Test cases 12-20 satisfy no additional constraints.
Problem credits: Andi Qu