Noisy Neighbors (Large)
Time limit5sMemory limit512 MB
Place N tenants on an R by C grid to minimize the number of shared walls between occupied neighbors.
- Level
Medium7 of 10
- Topics
- Combinatorics, Greedy, Matrix
- Solved
- No attempts yet
Problem
You are the landlord of an apartment building laid out as an by grid. Each apartment is one unit square cell with four walls. You want to rent of these apartments, one tenant per apartment, and leave the rest empty.
The trouble is that every prospective tenant is noisy. Whenever two occupied apartments share a wall, the building gains one point of unhappiness. Two apartments that meet only at a corner share no wall, so they add nothing. For example, a building with all four apartments occupied has four walls between neighbours, so its unhappiness is 4.
Place the tenants as well as possible and report the minimum unhappiness of the building.
Input
The first line contains the number of test cases . Each of the next lines contains three space separated integers , and .
Limits
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 possible unhappiness of that building.
Hint
A building with six tenants has all seven internal walls sitting between neighbours.
A building with two tenants has several placements in which no wall is shared.
A building with eight tenants is best filled as a ring around the empty centre cell.