1.
import
java.util.Scanner;
/**
*
FWP,
Ausgewählte Probleme aus dem ACM Programming Contest, SS10
* Problem: 11343 - Isolated Segments
* Link:
http://uva.onlinejudge.org/index.php?option=onlinejudge&page=show_problem&problem=2318
*
* @author Siegfried Ippisch
* @version 1.0
*
* Method : brute force
* Status : Accepted
*
Runtime:
0.544
*/
public class
IsolatedSegments {
static class Point{
int x,y;
public Point(int x, int y){
this.x = x; this.y = y;
}
public double distance(Point other){
double dx = this.x-other.x, dy = this.y-other.y;
return Math.sqrt(dx*dx+dy*dy);
}
public String toString(){
return "("+x+"|"+y+")";
}
}
static class Line{
Point
p1,p2;
public Line(Point p1, Point p2){
this.p1 = p1; this.p2 = p2;
}
public Line(int p1x, int p1y, int p2x, int p2y){
this(new Point(p1x,p1y), new Point(p2x,p2y));
}
public double length(){
return this.p1.distance(p2);
}
public boolean containsPoint(Point p){
return length() == p1.distance(p) + p2.distance(p);
}
public boolean hasIntersection(Line other){
double t1 = crossProduct(this.p1,other.p1,other.p2);
double t2 = crossProduct(this.p2,other.p1,other.p2);
double t3 = crossProduct(other.p1,this.p1,this.p2);
double t4 = crossProduct(other.p2,this.p1,this.p2);
//
Kreutzprodukte
müssen
jeweils unterschiedliches Vorzeichen haben
if(t1*t2 > 0) return false;
if(t3*t4 > 0) return false;
//
Sonderfall
-
Punkt und Linie ligen in einer Reihe:
if(t1
== 0 || t2 == 0 || t3 == 0 || t4 == 0)
//
überprüfen,
ob
ein Endpunkt auf der anderen Linien liegt
return this.containsPoint(other.p1) || this.containsPoint(other.p2) ||
other.containsPoint(this.p1) || other.containsPoint(this.p2);
return true;
}
public String toString(){
return p1+"-"+p2;
}
}
public static double crossProduct(Point m, Point l, Point r){
return (l.x-m.x)*(r.y-m.y) - (l.y-m.y)*(r.x-m.x);
}
public static int isolatedSegments(Line[] a){
int n = a.length;
int c = n;
for(int i=0; i<n; i++){
for(int j=0; j<n; j++)
if(j != i)
if(a[i].hasIntersection(a[j])){
c--;
break;
}
}
return c;
}
public static void main(String[] args){
Scanner
in
= new Scanner(System.in);
int n = in.nextInt();
while(n-- > 0){
int m = in.nextInt();
Line[]
segments
= new Line[m];
for(int i=0; i<m; i++)
segments[i]
=
new Line(in.nextInt(),in.nextInt(), in.nextInt(),
in.nextInt());
System.out.println(isolatedSegments(segments));
}
in.close();
}
}
2.
/**
* FWP,
Ausgewählte Probleme aus dem ACM Programming Contest
*
Problem: 11343 - Isolated Segments
*
Link:
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=25&page=show_problem&problem=2318
*
*
* Method
:
* Status
: Accepted
*
Runtime: 0.296
*
*
@author Felix Dietrich
*
@author Evgeni Pavlidis
*
@version 1.0, 31/10/2010
*/
import
java.io.*;
import
java.util.*;
class Point
{
int x,y;
Point(int x, int y)
{
this.x = x;
this.y = y;
}
public int hashCode()
{ return x << 16 + y; }
}
class Segment
{
static final double e = 1e-20;
Point p1, p2;
Segment(Point a1, Point a2)
{ p1 = a1; p2 = a2; }
boolean collidesWith(Segment o)
{
if(containsPoint(o.p1) || containsPoint(o.p2) ||
o.containsPoint(p1) || o.containsPoint(p2))
return true;
double t1 = crossProduct(p1,p2, o.p1) * crossProduct(p1,p2, o.p2);
double t2 = crossProduct(p2,p1, o.p1) * crossProduct(p2,p1, o.p2);
double t3 = crossProduct(o.p1,o.p2, p1) * crossProduct(o.p1,o.p2, p2);
double t4 = crossProduct(o.p2, o.p1, p1) * crossProduct(o.p2,o.p1, p2);
if(Math.abs(t1) < e && Math.abs(t2) < e &&
Math.abs(t3) < e && Math.abs(t4) < e)
return false;
if(t1 < e && t2 < e && t3 < e && t4
< e)
return true;
return false;
}
boolean containsPoint(Point p)
{
return Math.abs(dist(p1,p2) - (dist(p1,p) + dist(p2,p))) < e;
}
static double crossProduct(Point m, Point a, Point b)
{ return (a.x - m.x)*(b.y - m.y) - (b.x - m.x)*(a.y -
m.y); }
static double dist(Point a, Point b)
{ return Math.sqrt((a.x - b.x)*(a.x - b.x) + (a.y -
b.y)*(a.y - b.y)); }
}
class Main
{
static StreamTokenizer st = new StreamTokenizer(new BufferedReader(new
InputStreamReader(System.in)));
static int nextInt() throws IOException
{
st.nextToken();
return (int)st.nval;
}
public static void main(String...args) throws Exception
{
int testCases = nextInt();
int n;
Set<Segment> segments = new HashSet<Segment>();
Set<Segment> isolated = new HashSet<Segment>();
for(int tc = 1; tc <= testCases; tc++)
{
segments.clear();
isolated.clear();
n = nextInt();
for(int i = 0; i < n; i++)
segments.add(new Segment(new Point(nextInt(), nextInt()),
new Point(nextInt(), nextInt())) );
isolated = new HashSet<Segment>(segments);
for(Segment s1: segments)
for(Segment s2: segments)
if(s1 != s2 && s1.collidesWith(s2))
{
isolated.remove(s1);
isolated.remove(s2);
}
System.out.println(isolated.size());
}
}
}