Maximum Damage
Time limit1sMemory limit512 MB
On a grid with obstacles, orcs, and blank cells, pick at most T blank cells as stations so the total number of orcs within Manhattan distance R is maximized.
- Level
Medium5 of 10
- Topics
- Prefix sum, Brute force, Matrix
- Solved
- No attempts yet
Problem
The orcs and the elves have been at war for a long time, and the fighting has reached a decisive moment. The elves have located every orc camp and marked it on a map of the battlefield. To simplify their planning, the elves lay a coordinate grid over the battlefield and treat each unit square as a single point with integer coordinates, so every orc camp is described by a pair of integers (its x- and y-coordinates).
The elves want to build a limited number of Elf stations to strike the camps. Each station deals 1 unit of damage to every orc camp within distance of it, so the total damage a single station inflicts equals the number of orc camps within distance .
Because the elves' cavalry rides along horizontal and vertical trails laid out in a grid, distance is measured with the Manhattan metric rather than straight-line distance. For two points and ,
Some parts of the battlefield are too rugged to build on; the elves call such cells obstacles. The elves want to decide where to build a given number of Elf stations so as to inflict the maximum possible total damage on the orc camps.
The battlefield is a rectangular map in which every cell is one of three characters:
*— a blank cell, where an Elf station may be built;O— an orc camp;X— an obstacle, where no Elf station may be built.
An Elf station may be built only on a blank (*) cell, and each blank cell can hold at most one station. A station's damage is the number of orc camps whose Manhattan distance to it is at most . You place at most stations (fewer if there are fewer than blank cells) so as to maximize the sum of their damage. A legal battlefield map looks like this:
**O**
*OXO*
*****
**O**
Input
The first line contains the number of test cases (fewer than 15). The test cases follow one after another.
Each test case begins with a line of four integers , , , : and are the size of the battlefield along the y- and x-axes (the number of rows and columns), is the number of Elf stations you may place, and is each station's attack radius. The next lines each contain a string of characters describing one row of the map.
Output
For each test case, print a single line in the form
Maximum damage = X
where X is the maximum total damage that can be inflicted on the orc camps.