Manhattan Distance
Marlow Cold Store shifts crates with a gantry that runs on rails. The gantry travels along one axis at a time and never diagonally, so moving between two storage cells means running out along each axis in turn until every coordinate matches. The controller needs the total length of rail covered on that trip. Direction is irrelevant to the cost — running three metres left is the same three metres as running three metres right — and every axis has to be brought into line before the crate arrives.
Task: Print the total distance the gantry travels along its rails between the two cells.
Input
Three lines. The first holds n, the number of axes the gantry runs on. The second holds the first cell's n coordinates separated by single spaces. The third holds the second cell's n coordinates, in the same order. Coordinates may be negative or fractional.
Output
One line holding the total distance, written with exactly 4 digits after the decimal point.
Example:
Input:
3
1 2 3
4 6 3
Output:
7.0000
Sign in to solve this problem
Reading problems is free for everyone — solving them (Run, Submit, and tracking what you've solved) needs an account.
Sign in