Spare the Ewoks!
InterviewTime limit3sMemory limit128 MB
Given an m by n grid with blocked cells, choose up to three non-overlapping axis-aligned rectangles to maximize the total covered area.
- Level
Hard8 of 10
- Topics
- Dynamic programming, Prefix sum, Brute force
- Solved
- No attempts yet
Problem
The Galactic Empire wants to build a new base on the forest moon of Endor. Endor is home to a species of small, cuddly creatures called the Ewoks, and you have been ordered not to disturb any existing Ewok home. Subject to that, you must make the base as large as possible in total area, and that area may be split among up to three rectangular buildings.
You are given a map of Endor as an grid. Each cell is either empty or an Ewok home. Place up to three (possibly fewer) axis-aligned rectangular buildings on the grid so that no two buildings overlap and no building is placed on an Ewok home. Maximize the total area covered by the buildings.
Input
The input contains several test cases.
Each test case begins with a line containing two integers and (, ), the number of rows and columns of the grid. The next lines describe the map; each line contains exactly characters, where . (a period) is an empty cell and e (a lowercase letter e) is an Ewok home.
The input ends with a line containing 0 0, which must not be processed.
Output
For each test case, print a single line containing one integer: the maximum total area that can be covered by up to three non-overlapping rectangular buildings that avoid every Ewok home.