Monument
Time limit5sMemory limit128 MB
Find the maximum surface area 4ab of an axis-aligned a x a x b box of all-normal unit cubes that fits in a 3D grid with pores, where the square face can align with any of three axes.
- Level
Hard8 of 10
- Topics
- Binary search, Matrix, Dynamic programming, Geometry
- Solved
- No attempts yet
Problem
A millionaire wants to build a monument for her family; the names of her ancestors and descendants will be inscribed on its sides.
The monument is an axis-aligned rectangular block whose top and bottom faces are a x a squares and whose height is b. Each of its four sides is therefore an a x b rectangle, and the total area available for names is 4ab. We want this area to be as large as possible.
The monument is cut from a special p x q x r stone block. The stone is crystallised into a regular cubic grid, so it can be seen as p * q * r unit cubes (1 x 1 x 1). The finished monument is also made of whole unit cubes, and the stone may only be cut along the planes between unit cubes, perpendicular to the x-, y-, or z-axis. Thus the monument is a solid axis-aligned box of unit cubes, and its square face may lie in any of the three coordinate planes.
Some unit cubes of the stone are pores (empty). The monument must be flawless: it may not contain a single pore. Given a 3-D map of the stone that marks every unit cube as normal or pore, choose the sizes a and b so that:
- an
a x a x bblock can be cut from the stone using only normal cubes, and - the total side area
4abis as large as possible.
Input
The first line contains three positive integers p, q and r, separated by single spaces.
The next p * q lines each contain exactly r characters. Each character is either N (a normal unit cube) or P (a pore).
Number these p * q lines from 1 to p * q. Line k describes the column at
x = ((k - 1) mod p) + 1,y = floor((k - 1) / p) + 1,
and the z-th character of line k (for 1 <= z <= r) is the unit cube at coordinates (x, y, z), where 1 <= x <= p, 1 <= y <= q, and 1 <= z <= r.
Output
Print a single line containing the maximum possible value of 4ab. If the stone contains no normal cube, print 0.
Constraints
0 < p, q, r <= 150- Every character of the map is either
N(normal) orP(pore).