| <p> | |
| Wilson has had quite enough of his job driving trucks for a moving company, and is | |
| now interested in carrying out different sorts of deliveries — namely, pizza pie deliveries! | |
| Unfortunately, his local pizzeria already has a full-time driver, Mario Crosta, | |
| but Wilson has devised a devious plan to sabotage Mario, hopefully resulting in him | |
| getting fired and Wilson taking over. | |
| </p> | |
| <p> | |
| The pizzeria is located in a town which can be modeled as a grid of cells with | |
| <strong>N</strong> rows and <strong>M</strong> columns, with each cell representing an intersection. | |
| There are at least two intersections (in other words, <strong>N</strong> * <strong>M</strong> ≥ 2). | |
| It's possible to drive from a given intersection to any adjacent intersection (directly up, down, left, or right) | |
| in 1 minute. | |
| It's not possible to leave the grid. | |
| </p> | |
| <p> | |
| The pizzeria is located at the top-left intersection, and Wilson is planning on placing an order for pizza to | |
| be delivered to the bottom-right intersection. | |
| Due to the paramount importance of pizza arriving while it's still hot, this pizzeria guarantees that their deliveries | |
| will always arrive within at most <strong>N</strong> + <strong>M</strong> - 2 minutes (or the customer | |
| will get their money back and also the driver will be immediately fired). | |
| Ordinarily, this is just enough time to reach even the bottom-right corner of the town, but Wilson | |
| intends to make this not an ordinary delivery. | |
| </p> | |
| <p> | |
| Before placing his order, Wilson will pull some strings and arrange to have some of the intersections blocked off. | |
| In particular, he can shut down an entire axis-aligned <strong>K</strong>x<strong>K</strong> square of intersections all at once. | |
| He can even choose multiple such squares, as long as all of them lie entirely within the grid, | |
| none of them overlap with one another, and none of them include the top-left or bottom-right intersections. | |
| When he's done, Mario will be unable to pass through any intersection which is part of any of the squares. | |
| </p> | |
| <p> | |
| Wilson doesn't want to block off so many intersections that Mario will be unable to complete his delivery at all, | |
| as that would likely be too obvious. Instead, he wants to make sure that the delivery is still possible but is forced to take longer than | |
| <strong>N</strong> + <strong>M</strong> - 2 minutes, resulting in Mario losing his job. | |
| </p> | |
| <p> | |
| Despite his vast network of connections with corrupt government officials, shutting down each square of intersections | |
| will still cost Wilson a fair sum of money, so he'd like to successfully complete his sabotage by blocking off as few | |
| squares as possible. Unfortunately, it also might be the case that no possible set of squares can result in | |
| Mario's delivery taking too long without making the delivery impossible. | |
| </p> | |
| <p> | |
| On the 8x8 grid below, for example, if Wilson blocks off six 2x2 squares of intersections as shown (in black), | |
| the shortest possible delivery route from the pizzeria to the bottom-right intersection (in red) | |
| will take 22 minutes, which is longer than 8 + 8 - 2 = 14 minutes. | |
| However, note that this solution is not optimal. | |
| </p> | |
| <img src={{PHOTO_ID:261299628624564}} /> | |
| <h3>Input</h3> | |
| <p> | |
| Input begins with an integer <strong>T</strong>, the number of different pizzerias Wilson wants to work for. | |
| For each pizzeria, there is a single line containing the three space-separated integers, | |
| <strong>N</strong>, <strong>M</strong>, and <strong>K</strong>. | |
| </p> | |
| <h3>Output</h3> | |
| <p> | |
| For the <em>i</em>th truck, print a line containing "Case #<strong>i</strong>: " | |
| followed by the minimum number of blocked-off squares required for Wilson to successfully sabotage Mario (or -1 if it can't be done). | |
| </p> | |
| <h3>Constraints</h3> | |
| <p> | |
| 1 ≤ <strong>T</strong> ≤ 10,000 <br /> | |
| 1 ≤ <strong>N</strong>, <strong>M</strong>, <strong>K</strong> ≤ 800,000 <br /> | |
| </p> | |
| <h3>Explanation of Sample</h3> | |
| <p> | |
| In the first case, if both the top-right and bottom-left intersections are blocked off, | |
| then the bottom-right corner will be unreachable from the pizzeria, which is no good. | |
| If only one of those cells is blocked off, then Mario can proceed through the other one, | |
| managing to still complete his delivery in 2 + 2 - 2 = 2 minutes. | |
| Therefore Wilson has no way to sabotage him successfully. | |
| </p> | |