Invader Jina
InterviewTime limit2sMemory limit256 MB
Place two poison sources on empty cells of an N by M grid so that the maximum Manhattan distance from any village to its nearest source is minimized.
- Level
Medium6 of 10
- Topics
- Brute force, Math, Prefix sum, Binary search
- Solved
- No attempts yet
Problem
The planet is an by grid. Each cell is either empty () or a village (). You must place exactly poison bags, each on an empty cell.
Every second, the poison spreads to the four neighboring cells (up, down, left, right). A poisoned village infects each adjacent village after second, so the spread is equivalent to movement through every cell of the grid. A village at becomes poisoned at the smaller of its Manhattan distances to the two bags, and the time to poison every village is the maximum of those values over all villages. Find the placement that minimizes this maximum.
Rows are numbered from to and columns from to , with the top-left cell at . The input always admits a placement.
Input
The first line contains two integers and separated by a space. ()
Each of the following lines contains a string of length . Each character is or , where means an empty cell and means a village.
Output
Print the minimum time in seconds needed to poison every village when the poison bags are placed on the best empty cells.