Parcels
Time limit15sMemory limit1024 MB
Given a grid of delivery offices, find the one extra office placement that minimizes the largest Manhattan distance from any square to its nearest office.
- Level
Medium6 of 10
- Topics
- Matrix, Math, Dynamic programming
- Solved
- No attempts yet
Problem
You were recently hired as the Chief Decision Maker (CDM) at a well-known parcel delivery company. Congratulations! Customers love fast deliveries, so you have decided to cut the time it takes to deliver parcels around the world. You presented this idea to the authorities, and they gave you a budget to build at most one new delivery office.
The world is divided into an R × C grid of squares. Each square either has a delivery office or does not. You may pick a square without a delivery office and build a new one there.
The delivery time to a square is 0 if the square has a delivery office. Otherwise, it is the minimum Manhattan distance from that square to any other square with a delivery office. The overall delivery time is the maximum delivery time over all squares. What is the minimum overall delivery time you can get by building at most one new delivery office?
Note: The Manhattan distance between squares and is .
Input
The first line contains the number of test cases . Each test case begins with a line containing the number of rows and columns of the grid. Each of the next lines contains a string of characters, each either 0 (no delivery office in that square) or 1 (a delivery office is in that square).
Output
For each test case, print one line in the form Case #x: y, where is the test case number (starting from 1) and is the minimum overall delivery time you can get after adding at most one delivery office.
Constraints
. The initial grid has at least one delivery office.
Hint
In Sample Case #1, building a new delivery office in any of the five squares without one gives a minimum overall delivery time of 1. In Sample Case #2, every square already has a delivery office, so the minimum overall delivery time is 0. You may add at most one office, so adding none is also allowed. In Sample Case #3, the overall delivery time is 2 if you build in (2, 3), (3, 2), (3, 3), (3, 4), or (4, 3). Any other square gives a higher overall delivery time.