The dog task
The dog task |
Hunter Bob often walks with his dog Ralph. Bob walks with a constant speed and his route is a polygonal line (possibly self-intersecting) whose vertices are specified byN pairs of integers (Xi, Yi) - their Cartesian coordinates.
Ralph walks on his own way but always meets his master at the specified Npoints. The dog starts his journey simultaneously with Bob at the point (X1,Y1) and finishes it also simultaneously with Bob at the point (XN , YN).
Ralph can travel at a speed that is up to two times greater than his master's speed. While Bob travels in a straight line from onepoint to another the cheerful dog seeks trees, bushes, hummocks and all other kinds ofinteresting places of the local landscapewhich are specified by M pairs of integers (X'j,Y'j). However, after leaving his master at the point (Xi ,Yi) (where )the dog visits at most one interesting place before meeting his master again at the point (Xi+1 , Yi+1).
Your task is to find the dog's route, which meets the above requirements and allows him to visit the maximal possible numberof interesting places. The answer should be presented as a polygonal line that represents Ralph's route. The vertices of thisroute should be all points (Xi , Yi) and the maximal number ofinteresting places (X'j,Y'jX). The latter should be visited (i.e.listed in the route description) at most once.
An example of Bob's route (solid line), a set of interesting places (dots) and one of the best Ralph's routes (dotted line) arepresented in the following picture:
Input
The first line of the input is an integer L, then a blank line followed by L datasets. There is a blank line between datasets.The first line of each dataset contains two integers N and M, separatedby a space (). The secondline contains N pairs of integers ,separated byspaces, that represent Bob's route. The third line contains M pairs ofintegers ,separated by spaces, that represent interesting places.
All points in the input file are different and their coordinates are integersnot greater than 1000 by the absolute value.
Output
The first line of each dataset should contain the single integer K - thenumber of vertices of the best dog's route. The second line should contain Kpairs of coordinates ,separated by spaces, that represent this route. If there areseveral such routes, then you may write any of them.Print a blank line between datasets.
Sample Input
1 4 5 1 4 5 7 5 2 -2 4 -4 -2 3 9 1 2 -1 3 8 -3
Sample Output
6 1 4 3 9 5 7 5 2 1 2 -2 4
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
import java.util.Arrays;
public class Main
{
public static final boolean DEBUG = false;
public static final double EPS = 1e-6;
public StreamTokenizer tokenizer;
public BufferedReader cin;
public PrintWriter cout;
public int[] leftx, lefty, rightx, righty;
public int[] match;
public int n, m;
public int[][] g;
public boolean[] vis;
public int ans;
public void init()
{
try {
if (DEBUG) {
cin = new BufferedReader(new InputStreamReader(
new FileInputStream("d:\\OJ\\uva_in.txt")));
} else {
cin = new BufferedReader(new InputStreamReader(System.in));
}
cout = new PrintWriter(new OutputStreamWriter(System.out));
tokenizer = new StreamTokenizer(cin);
} catch (Exception e) {
e.printStackTrace();
}
}
public String next()
{
try {
tokenizer.nextToken();
if (tokenizer.ttype == StreamTokenizer.TT_EOF) return null;
else if (tokenizer.ttype == StreamTokenizer.TT_NUMBER) return String.valueOf((int)tokenizer.nval);
else if (tokenizer.ttype == StreamTokenizer.TT_WORD) return tokenizer.sval;
else return null;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public double dis(int x1, int y1, int x2, int y2)
{
double x = x1 - x2;
double y = y1 - y2;
return Math.sqrt(x * x + y * y);
}
public boolean input()
{
n = Integer.parseInt(next());
m = Integer.parseInt(next());
rightx = new int[n];
righty = new int[n];
leftx = new int[m];
lefty = new int[m];
for (int i = 0; i < n; i++) {
rightx[i] = Integer.parseInt(next());
righty[i] = Integer.parseInt(next());
}
for (int i = 0; i < m; i++) {
leftx[i] = Integer.parseInt(next());
lefty[i] = Integer.parseInt(next());
}
g = new int[m][n];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n - 1; j++) {
double d1 = dis(leftx[i], lefty[i], rightx[j], righty[j]);
double d2 = dis(leftx[i], lefty[i], rightx[j + 1], righty[j + 1]);
double d = dis(rightx[j], righty[j], rightx[j + 1], righty[j + 1]);
if (d1 + d2 - 2 * d < EPS) g[i][j] = 1;
}
}
return true;
}
public boolean find(int u)
{
for (int v = 0; v < n; v++) {
if (g[u][v] != 0 && !vis[v]) {
vis[v] = true;
if (match[v] == -1 || find(match[v])) {
match[v] = u;
return true;
}
}
}
return false;
}
public void solve(int cas)
{
vis = new boolean[n];
ans = 0;
match = new int[n];
Arrays.fill(match, -1);
for (int i = 0; i < m; i++) {
Arrays.fill(vis, false);
if (find(i)) ans++;
}
cout.println(ans + n);
for (int i = 0; i < n; i++) {
if (i == 0) cout.print(rightx[i] + " " + righty[i]);
else cout.print(" " + rightx[i] + " " + righty[i]);
int v = match[i];
if (v != -1) cout.print(" " + leftx[v] + " " + lefty[v]);
}
cout.println();
if (cas != 0) cout.println();
cout.flush();
}
public static void main(String[] args)
{
Main solver = new Main();
solver.init();
int t = Integer.parseInt(solver.next());
while (t-- > 0) {
solver.input();
solver.solve(t);
}
}
}