UVa273 - Jack Straws(线段相交,floyd_warshall)

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 integerstex2html_wrap_inline34 , tex2html_wrap_inline36 , tex2html_wrap_inline38 and tex2html_wrap_inline40 , giving the coordinates, tex2html_wrap_inline42 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 
CONNECTED
1、两个线段是否相交

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);
		}
	}
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kgduu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值