1+ import java .io .*;
2+ import java .util .*;
3+ public class cowjump {
4+ public static void testIntersections () {
5+ assert Point .intersection (new Point (0 ,0 ), new Point (2 ,9 ), new Point (0 ,1 ), new Point (6 ,1 ))== true ;
6+ assert Point .intersection (new Point (0 ,0 ), new Point (1 ,1 ), new Point (3 ,3 ), new Point (3 ,12 ))== false ;
7+ System .out .println ("All Tests OK!" );
8+ }
9+ public static void main (String [] args ) throws IOException {
10+ // TODO Auto-generated method stub
11+ //testIntersections();
12+ BufferedReader f = new BufferedReader (new FileReader ("cowjump.in" ));
13+ int N = Integer .parseInt (f .readLine ());
14+ Point [][] input = new Point [N ][2 ];
15+ //System.out.println(input[0][0]);
16+ for (int i = 0 ; i < N ; i ++) {
17+ StringTokenizer st = new StringTokenizer (f .readLine ());
18+ input [i ][0 ] = new Point (Integer .parseInt (st .nextToken ()), Integer .parseInt (st .nextToken ()));
19+ input [i ][1 ] = new Point (Integer .parseInt (st .nextToken ()), Integer .parseInt (st .nextToken ()));
20+ for (int j = 0 ; j < i ; j ++) {
21+ if (Point .intersection (input [j ][0 ], input [j ][1 ], input [i ][0 ], input [i ][1 ])) {
22+ PrintWriter pw = new PrintWriter ("cowjump.out" );
23+ pw .println (i +1 );
24+ pw .close ();
25+ System .exit (0 );
26+ }
27+ }
28+ }
29+ }
30+
31+ }
32+ class Point {
33+ int x ,y ;
34+ public Point (int x ,int y ) {
35+ this .x = x ;
36+ this .y = y ;
37+ }
38+ public Point () {
39+ this .x = 0 ;
40+ this .y = 0 ;
41+ }
42+ static boolean intersection (Point a , Point b ,Point c , Point d ) {
43+ Point E = new Point (b .x - a .x , b .y - a .y );
44+ Point F = new Point (d .x - c .x , d .y - c .y );
45+ Point P = new Point (-E .y , E .x );
46+ Point Q = new Point (a .x - c .x , a .y - c .y );
47+ double k = F .x * P .x + F .y * P .y ;
48+ if (k == 0 ) {
49+ // Parallel
50+ return false ;
51+ }
52+
53+ double h = (Q .x * P .x + Q .y * P .y )/(k );
54+ if (0 <= h && h <= 1 ) {
55+ return true ;
56+ }
57+ return false ;
58+ }
59+ }
0 commit comments