Jack Straws
Jack Straws |
In the game of Jack Straws, a number of plastic or wooden ``straws" are dumped on the table and players try to remove them one-by-one without disturbing the other straws. Here, we are only concerned with if various pairs of straws are connected by a path of touching straws. You will be given a list of the endpoints for some straws (as if they were dumped on a large piece of graph paper) and then will be asked if various pairs of straws are connected. Note that touching is connecting, but also two straws can be connected indirectly via other connected straws.
Input
The input begins with a single positive integer on a line by itself indicating the number of the cases following, each of them as described below. This line is followed by a blank line, and there is also a blank line between two consecutive inputs.
A test case consists of multiple lines of input. The first line will be an integer n (1 < n < 13) giving the number of straws on the table. Each of the next n lines contain 4 positive integers, , , and , giving the coordinates, of the endpoints of a single straw. All coordinates will be less than 100. (Note that the straws will be of varying lengths.) The first straw entered will be known as straw #1, the second as straw #2, and so on. The remaining lines of input (except for the final line) will each contain two positive integers, a and b, both between 1 and n, inclusive. You are to determine if straw a can be connected to straw b. When a = 0 = b, the input is terminated.
There will be no illegal input and there are no zero-length straws.
Output
For each test case, the output must follow the description below. The outputs of two consecutive cases will be separated by a blank line.
You should generate a line of output for each line containing a pair a and b, except the final line where a= 0 = b. The line should say simply ``CONNECTED", if straw a is connected to straw b, or ``NOT CONNECTED", if straw a is not connected to straw b. For our purposes, a straw is considered connected to itself.
Sample Input
1 7 1 6 3 3 4 6 4 9 4 5 6 7 1 4 3 5 3 5 5 5 5 2 6 3 5 4 7 2 1 4 1 6 3 3 6 7 2 3 1 3 0 0
Sample Output
CONNECTED NOT CONNECTED CONNECTED CONNECTED NOT CONNECTED CONNECTED1、两个线段是否相交
2、所有点对之间最短距离
import java.io.FileInputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.OutputStreamWriter;
import java.io.StreamTokenizer;
public class Main {
private static final boolean DEBUG = false;
private BufferedReader cin;
private PrintWriter cout;
private StreamTokenizer tokenizer;
private Line[] lines;
private int n;
private boolean[][] g;
class Point
{
int x, y;
}
class Line
{
Point a, b;
}
public void init()
{
try {
if (DEBUG) {
cin = new BufferedReader(new InputStreamReader(
new FileInputStream("e:\\uva_in.txt")));
} else {
cin = new BufferedReader(new InputStreamReader(System.in));
}
tokenizer = new StreamTokenizer(cin);
cout = new PrintWriter(new OutputStreamWriter(System.out));
} catch (Exception e) {
e.printStackTrace();
}
}
private 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 return tokenizer.sval;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public boolean input()
{
n = Integer.parseInt(next());
lines = new Line[n];
for (int i = 0; i < n; i++) {
lines[i] = new Line();
lines[i].a = new Point();
lines[i].a.x = Integer.parseInt(next());
lines[i].a.y = Integer.parseInt(next());
lines[i].b = new Point();
lines[i].b.x = Integer.parseInt(next());
lines[i].b.y = Integer.parseInt(next());
}
return true;
}
private int cross(Point a, Point b, Point c, Point d)
{
int x1 = b.x - a.x, y1 = b.y - a.y;
int x2 = d.x - c.x, y2 = d.y - c.y;
return x1 * y2 - y1 * x2;
}
private boolean intersection(Line l1, Line l2)
{
return (Math.max(l1.a.x, l1.b.x) >= Math.min(l2.a.x, l2.b.x) &&
Math.max(l2.a.x, l2.b.x) >= Math.min(l1.a.x, l1.b.x) &&
Math.max(l1.a.y, l1.b.y) >= Math.min(l2.a.y, l2.b.y) &&
Math.max(l2.a.y, l2.b.y) >= Math.min(l1.a.y, l1.b.y) &&
cross(l1.a, l2.a, l1.a, l1.b) * cross(l1.a, l1.b, l1.a, l2.b) >= 0 &&
cross(l2.a, l1.a, l2.a, l2.b) * cross(l2.a, l2.b, l2.a, l1.b) >= 0);
}
private void floyd_warshall()
{
for (int k = 0; k < n; k++) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
g[i][j] |= g[i][k] & g[k][j];
}
}
}
}
public void solve(int cas)
{
g = new boolean[n][n];
for (int i = 0; i < n; i++) {
for (int j = i; j < n; j++) {
if (j == i) {
g[i][j] = g[j][i] = true;
continue;
}
if (intersection(lines[i], lines[j])) {
g[i][j] = g[j][i] = true;
}
}
}
floyd_warshall();
while (true) {
int a = Integer.parseInt(next());
int b = Integer.parseInt(next());
if (a == 0 && b== 0) break;
a--; b--;
if (g[a][b]) cout.println("CONNECTED");
else cout.println("NOT CONNECTED");
}
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);
}
}
}