Enclosure
Time limit5sMemory limit512 MB
Place the fewest stones on an N by M grid of at most 20 cells so at least K points are cut off from the border.
- Level
Medium4 of 10
- Topics
- Brute force, BFS, Bit manipulation
- Solved
- No attempts yet
Problem
A grid is made of horizontal lines and vertical lines. The horizontal and vertical lines meet at intersection points, and each intersection point holds at most one stone.
An intersection point is enclosed if either of the following holds.
- A stone is placed on the point.
- Starting from the point and moving along grid lines, you cannot reach an empty intersection point on the border of the grid by passing through empty intersection points only.
Each move goes along a grid line to a neighbouring intersection point. A path is usable only if its starting point, every point it passes, and its final point are all empty. An empty point on the border is therefore never enclosed.
Place stones so that at least points are enclosed. Find the minimum number of stones needed.
For example, enclosing 8 points on a grid needs at least 6 stones. The picture below shows one such layout, with the enclosed points marked x.

Input
The first line contains the number of test cases, . Each of the next lines contains three integers , and , separated by spaces.
Limits
Output
For each test case, print one line of the form Case #x: y, where is the test case number starting from 1 and is the minimum number of stones needed.