eolymp
bolt
Try our new interface for solving problems
Problems

Collecting Beepers

Collecting Beepers

Karel is a robot who lives in a rectangular coordinate system where each place is designated by a set of integer coordinates (\textbf{x} and \textbf{y}). Your job is to design a program that will help Karel pick up a number of beepers that are placed in her world. To do so you must direct Karel to the position where each beeper is located. Your job is to write a computer program that finds the length of the shortest path that will get Karel from her starting position, to each of the beepers, and return back again to the starting position. Karel can only move along the \textbf{x} and \textbf{y} axis, never diagonally. Moving from one position (\textbf{i}, \textbf{j}) to an adjacent position (\textbf{i}, \textbf{j+1}), (\textbf{i}, \textbf{j-1}), (\textbf{i-1}, \textbf{j}), or (\textbf{i+1}, \textbf{j}) has a cost of one. You can assume that Karel’s world is never larger than \textbf{20}×\textbf{20} squares and that there will never be more than \textbf{10} beepers to pick up. Each coordinate will be given as a pair (\textbf{x}, \textbf{y}) where each value will be in the range \textbf{1} through the size of that particular direction of the coordinate system. \InputFile First there will be a line containing the number of scenarios you are asked to help Karel in. For each scenario there will first be a line containing the size of the world. This will be given as two integers (\textbf{x}-size and \textbf{y}-size). Next there will be one line containing two numbers giving the starting position of Karel. On the next line there will be one number giving the number of beepers. For each beeper there will be a line containing two numbers giving the coordinates of each beeper. \OutputFile The output will be one line per scenario, giving the minimum distance that Karel has to move to get from her starting position to each of the beepers and back again to the starting position.
Time limit 1 second
Memory limit 64 MiB
Input example #1
1
10 10
1 1
4
2 3
5 5
9 4
6 5
Output example #1
The shortest path has length 24