Collecting Beepers
Time limit1sMemory limit128 MB
Given Karel's start and up to 8 beepers on a grid, find the shortest Manhattan-distance round trip visiting every beeper.
- Level
Medium6 of 10
- Topics
- Dynamic programming, Bit manipulation, Graph, Brute force
- Solved
- No attempts yet
Problem
Karel is a robot living in a rectangular coordinate system where every position is given by a pair of integer coordinates . A number of beepers are placed throughout her world, and Karel must pick all of them up. Karel can only move along the and axes, never diagonally. Moving to an adjacent position costs one unit of distance, so the travel distance between two positions equals their Manhattan distance (the sum of the absolute differences of their coordinates).
Karel starts at her starting position, must visit every position that holds a beeper, and then return to her starting position. Find the minimum total length of the path Karel travels. The beepers may be visited in any order.
Input
The first line contains the number of scenarios. Each scenario is given as follows:
- A line with two integers: the size of the world (x-size and y-size).
- A line with two integers and : Karel's starting position.
- A line with one integer : the number of beepers.
- following lines, each with two integers and : the coordinates of a beeper.
Output
For each scenario, print one line giving the minimum distance Karel must travel to leave her starting position, visit every beeper, and return to the starting position, in the form:
The shortest path has length D
where is the minimum distance.
Constraints
- size of the world
- number of beepers
- size of the world