Editorial for COCI '06 Contest 2 #5 Stol
Submitting an official solution before solving the problem yourself is a bannable offence.
Suppose one dimension of the table is fixed. For example, let the table start in column
For each row we can determine if there are any blocked squares between columns
A single pass finds the largest consecutive number of rows that don't have walls between
The number of possibilities is
Time complexity:
Comments
There is also an
solution to this problem by reducing the problem to largest rectangle in a histogram, and then solving with a monotonic stack. The only modification you need is to use the formula for perimeter instead of area. Example: https://dmoj.ca/src/5742708
(You need this method to pass with Python.)