PizzaDelivery
SRM 451 · 2009-10-20 · by vexorian
Problem Statement
From each square in the grid, you can only move to adjacent squares. Two squares are adjacent if they share an edge. You can only move between two empty squares if the absolute difference of their heights is less than or equal to 1. If the height difference is 0, it takes 1 minute to make the move, and if the absolute height difference is 1, it takes 3 minutes.
You can always move to a building from any of its adjacent squares and vice versa, regardless of height. This is because all buildings are taller than the highest terrain, and each building has entrances and exits for all its adjacent squares at the correct heights. Moving to or from a square containing a building takes 2 minutes. The delivery boys are allowed to enter buildings even if they are not their final destinations. Note that the pizza place itself is also a building.
Each delivery boy can only carry one pizza at a time. This means that after each delivery, the delivery boy must return to the pizza place to pick up another pizza if there are more deliveries left to do. You are given a
Constraints
- terrain will contain between 2 and 50 elements, inclusive.
- Each element of terrain will contain between 2 and 50 characters, inclusive.
- Each element of terrain will contain the same number of characters.
- Each character in terrain will be 'X', '$' or a digit between '0' and '9', inclusive.
- terrain will contain between 1 and 20 '$' characters, inclusive.
- terrain will contain exactly one 'X' character.
{"3442211",
"34$221X",
"3442211"}
Returns: 8
Only one pizza boy is needed for this single delivery. The pizza boy must first take two minutes to go from the restaurant to the cell to its left. Then he must climb from the cell of height '1' to the left cell of height '2' , taking 3 minutes. The movement between two cells of height '2' takes one minute. He finally needs two more minutes to go from the cell of height '2' to the only building in the area.
{"001000$",
"$010X0$",
"0010000"}
Returns: 13
This time there are three buildings, and an optimal solution is as follows: 00:00 -> Pizza boy #1 takes the pizza assigned for the left building. 00:00 -> Pizza boy #2 takes the pizza assigned for the right building. 00:04 -> Pizza boy #2 delivers the pizza to the right building. 00:08 -> Pizza boy #2 arrives back at the restaurant, takes the pizza for the top-right building. 00:10 -> Pizza boy #1 delivers the pizza to the left building. 00:13 -> Pizza boy #2 delivers the pizza to the top-right building.
{"001000$",
"$010X0$",
"0010000",
"2232222",
"2222222",
"111$111"}
Returns: -1
The irregular terrain blocks deliveries to the bottom building.
{"001000$",
"$010X0$",
"0010000",
"1232222",
"2222222",
"111$111"}
Returns: 28
This time, there is a possible path connecting the restaurant and the bottom building.
{"X$$",
"$$$"}
Returns: 14
Submissions are judged against all 73 archived test cases, of which 5 are shown here. Case numbers match the judge’s.
Language: C++17 · define a public class PizzaDelivery with a public method int deliverAll(vector<string> terrain) · 73 test cases · 2 s / 256 MB per case