#K15391. Minimal Time to Pick Mushrooms
Minimal Time to Pick Mushrooms
Minimal Time to Pick Mushrooms
You are given a rectangular grid of size \(m \times n\) representing a forest. The starting position is at the top-left corner of the grid, i.e. at coordinate \((0,0)\). There are \(k\) mushrooms placed in the forest at specified coordinates. Your task is to determine the minimal time required to pick all the mushrooms and return back to the starting position.
You can move in four directions: up, down, left, and right. Each move costs 1 unit of time. The distance between two points \((x_1, y_1)\) and \((x_2, y_2)\) in an open grid is equal to their Manhattan distance: \( |x_1 - x_2| + |y_1 - y_2| \).
You must plan a route that starts at \((0,0)\), visits all mushroom locations, and finally returns to \((0,0)\), while minimizing the total time (or distance) traveled.
inputFormat
The first line of input contains three integers \(m\), \(n\), and \(k\): the number of rows, the number of columns, and the number of mushrooms, respectively.
The following \(k\) lines each contain two integers, representing the row and column coordinates of a mushroom.
Note: Coordinates are zero-indexed.
outputFormat
Output a single integer: the minimal time required to collect all mushrooms and return to the starting position.
## sample4 4 3
1 2
2 1
3 3
12