This repository was archived by the owner on Feb 29, 2024. It is now read-only.
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcowjump.java
More file actions
Latest commit
394 lines (371 loc) · 11.2 KB
/
Copy pathcowjump.java
File metadata and controls
394 lines (371 loc) · 11.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
importjava.io.*;
importjava.util.*;
publicclasscowjump {
publicstaticintpointYCompare(Pointp1, Pointp2) {
// Compare points
// Check if p2 is above, below, or next to p1
System.out.println("pointycompare(("+p1.x+","+p1.y+"),("+p2.x+","+p2.y+")");
if(p1.y == p2.y) {
System.out.println("output: 0"); // Same Y
return0;
}
if(p2.y < p1.y) {
System.out.println("output: -1"); // below
return -1;
}
if(p2.y > p1.y) {
System.out.println("output: 1"); // above
return1;
}
System.out.println("NONE OF THE ABOVE"); // This code should never run, but just keeping this to prevent syntax errors
return -9999999;
}
publicstaticintlinesCompare(LineSegementm, LineSegementl) {
intoutput = pointYCompare(m.a, l.a) * pointYCompare(m.b, l.b);;
System.out.println("Intersection of line "+m+" and "+l + " is "+output + " == -1");
returnoutput;
}
publicstaticvoidtestIntersections() {
assertPoint.intersection(newPoint(0,0), newPoint(2,9), newPoint(0,1), newPoint(6,1))== true;
//assert Point.intersection(new Point(0,0), new Point(1,1), new Point(3,3), new Point(3,12))== false;
assertlinesCompare(newLineSegement(newPoint(0,0), newPoint(2,3)),newLineSegement(newPoint(0,3),newPoint(9,1))) == -1;
System.out.println("All Tests OK!");
}
publicstaticbooleansweepCheck(LineSegements,Point[][] input) {
for(inti = 0; i < input.length; i ++) {
System.out.println("Checking line "+i);
if(input[i][0] == null && input[i][1] == null) {
System.out.println("End of segments");
break;
}
System.out.println("Checking "+s.a.x+" - "+input[i][0].x + " - "+s.b.x);
booleanfirstWithinLine = (s.a.x <= input[i][0].x && s.b.x >= input[i][0].x);
System.out.println("(s.a.x <= input[i][0].x && s.b.x >= input[i][0].x)");
System.out.println((s.a.x <= input[i][0].x)+" && " + (s.b.x >= input[i][0].x));
System.out.println("firstWithinLine = "+firstWithinLine);
//|| (s.a.y <= input[i][0].y && s.b.y >= input[i][0].y);
if(firstWithinLine) {
// TODO check line cross logic
LineSegementfull = newLineSegement(input[i][0], input[i][1]);
if(linesCompare(s,newLineSegement(full.atX_(s.a.x),full.atX_(s.a.y))) == -1) {
System.out.println("Intersect!");
returntrue;
}else {
System.out.println("No Intersection!");
}
//continue; // Both statements may be true
}
booleansecondWithinLine = (s.a.x <= input[i][1].x && s.b.x >= input[i][1].x);
//|| (s.a.y <= input[i][0].y && s.b.y >= input[i][0].y);
if(secondWithinLine && !firstWithinLine) {
// TODO check line cross logic
LineSegementfull = newLineSegement(input[i][0], input[i][1]);
if(linesCompare(s,newLineSegement(full.atX_(s.a.x), full.atX_(s.b.x))) == -1) {
System.out.println("Intersect!");
returntrue;
}else {
System.out.println("No Intersection!");
}
}
}
returnfalse;
}
publicstaticvoidmain(String[] args) throwsIOException{
//testIntersections();
//testIntersections();
BufferedReaderf = newBufferedReader(newFileReader("cowjump2.in"));
intN = Integer.parseInt(f.readLine());
Point[][] input = newPoint[N][2];
//System.out.println(input[0][0]);
intoutput = -1;
for(inti = 0; i < N; i ++) {
StringTokenizerst = newStringTokenizer(f.readLine());
<<<<<<< HEAD
<<<<<<< HEAD
<<<<<<< HEAD
<<<<<<< HEAD
Pointa = newPoint(Integer.parseInt(st.nextToken()), Integer.parseInt(st.nextToken()));
Pointb = newPoint(Integer.parseInt(st.nextToken()), Integer.parseInt(st.nextToken()));
booleanstatus;
if(a.x > b.x) {
status = sweepCheck(
newLineSegement(b,a),
input);
}else {
status = sweepCheck(
newLineSegement(a,b),
input);
}
System.out.println("That was line "+i);
output = i;
if(status) {
break;
}
input[i][0] = a;
input[i][1] = b;
/*for(int j = 0; j < i; j ++) {
if(Point.intersection(input[j][0], input[j][1], input[i][0], input[i][1])) {
PrintWriter pw = new PrintWriter("cowjump.out");
pw.println(i+1);
pw.close();
System.exit(0);
=======
=======
>>>>>>> parent of c6da410... Long Proofing
=======
>>>>>>> parent of c6da410... Long Proofing
=======
>>>>>>> parent of c6da410... Long Proofing
a = new Point(Integer.parseInt(st.nextToken()),Integer.parseInt(st.nextToken()));
b = new Point(Integer.parseInt(st.nextToken()),Integer.parseInt(st.nextToken()));
a = a.setIndex(i);
b = b.setIndex(i);
if(a.compareTo(b) == 1) {
lookup[i][0] = b;
lookup[i][1] = a;
}else {
lookup[i][0] = a;
lookup[i][1] = b;
}
points.add(a);
points.add(b);
segements.add(new LineSegement(a, b));
}
f.close();
points.sort(null);
// Algorthim
int size = points.size();
for(int i = 0; i < size; i ++) {
System.out.println(points.get(i));
}
for(int i =0 ; i < N; i ++) {
System.out.println(Arrays.toString(lookup[i]));
System.out.println(segements.get(i));
}
int currentY = -1;
int index;
// boolean newY = true;
List<Integer> pz = new ArrayList<Integer>();
int[] tbl = new int[N];
int max = -1;
int maxi = -1;
for(int i = 0 ; i < N; i ++) {
Point p = points.get(i);
//index = p.index;
/*if(p.y != currentY) {
currentY = (int) p.y;
continue;
}else {
*/
intstate = num(p.index,p,lookup);
System.out.println("Endpoint "+ state + " "+p);
if(state == 0) {
pz.add(p.index);
intsz = pz.size() - 1;
LineSegementnewline = segements.get(p.index);
for(intj =0 ;j < sz; j ++ ) {
if(Point.intersection(newline,segements.get(pz.get(j)))) {
System.out.println("Intersectsion at "+segements.get(p.index)+" -|||- "+segements.get(j));
tbl[i] ++;
tbl[j] ++;
if(tbl[i] > max) {
max = tbl[i];
maxi = i;
}
if(tbl[j] > max) {
max = tbl[j];
maxi = j;
}
}
}
}elseif(state == 1) {
pz.remove(pz.indexOf(p.index));
}else {
continue;
>>>>>>> parentofc6da410... LongProofing
}
}*/
}
f.close();
PrintWriterpw = newPrintWriter("cowjump.out");
pw.println(output+1);
pw.close();
System.exit(0);
}
<<<<<<< HEAD
<<<<<<< HEAD
}
}
=======
}
>>>>>>> parentof84bd17b... Mergebranch'master'ofhttps://github.com/javaarchive/Java
classPoint{
doublex,y;
=======
}
classPointimplementsComparable<Point>{
doublex,y;
intindex = -1;
<<<<<<< HEAD
<<<<<<< HEAD
<<<<<<< HEAD
>>>>>>> parentofc6da410... LongProofing
=======
>>>>>>> parentofc6da410... LongProofing
=======
>>>>>>> parentofc6da410... LongProofing
=======
>>>>>>> parentofc6da410... LongProofing
publicPoint(doublex,doubley) {
this.x = x;
this.y = y;
}
publicPoint(intx,inty) {
this.x = x;
this.y = y;
}
publicPoint() {
this.x = 0;
this.y = 0;
}
staticbooleanintersection(Pointa, Pointb,Pointc, Pointd) {
// OLD CALCULATION CODE
PointE = newPoint(b.x - a.x, b.y - a.y);
PointF = newPoint(d.x - c.x, d.y - c.y);
PointP = newPoint(-E.y, E.x);
PointQ = newPoint(a.x - c.x, a.y - c.y);
doublek = F.x * P.x + F.y * P.y;
if(k == 0) {
// Parallel
returnfalse;
}
doubleh = (Q.x * P.x + Q.y * P.y)/(k);
if(0 <= h && h <= 1) {
returntrue;
}
returnfalse;
}
<<<<<<< HEAD
publicStringtoString() {
return"("+this.x + ","+ this.y + ")";
=======
// Given three colinear points p, q, r, the function checks if
// point q lies on line segment 'pr'
staticbooleanonSegment(Pointp, Pointq, Pointr)
{
if (q.x <= Math.max(p.x, r.x) && q.x >= Math.min(p.x, r.x) &&
q.y <= Math.max(p.y, r.y) && q.y >= Math.min(p.y, r.y))
returntrue;
returnfalse;
}
// To find orientation of ordered triplet (p, q, r).
// The function returns following values
// 0 --> p, q and r are colinear
// 1 --> Clockwise
// 2 --> Counterclockwise
staticintorientation(Pointp, Pointq, Pointr)
{
// See https://www.geeksforgeeks.org/orientation-3-ordered-points/
// for details of below formula.
doubleval = (q.y - p.y) * (r.x - q.x) -
(q.x - p.x) * (r.y - q.y);
if (val == 0) return0; // colinear
return (val > 0)? 1: 2; // clock or counterclock wise
}
// The main function that returns true if line segment 'p1q1'
// and 'p2q2' intersect.
staticbooleanintersection(Pointp1, Pointq1, Pointp2, Pointq2)
{
// Find the four orientations needed for general and
// special cases
into1 = orientation(p1, q1, p2);
into2 = orientation(p1, q1, q2);
into3 = orientation(p2, q2, p1);
into4 = orientation(p2, q2, q1);
// General case
if (o1 != o2 && o3 != o4)
returntrue;
// Special Cases
// p1, q1 and p2 are colinear and p2 lies on segment p1q1
if (o1 == 0 && onSegment(p1, p2, q1)) returntrue;
// p1, q1 and q2 are colinear and q2 lies on segment p1q1
if (o2 == 0 && onSegment(p1, q2, q1)) returntrue;
// p2, q2 and p1 are colinear and p1 lies on segment p2q2
if (o3 == 0 && onSegment(p2, p1, q2)) returntrue;
// p2, q2 and q1 are colinear and q1 lies on segment p2q2
if (o4 == 0 && onSegment(p2, q1, q2)) returntrue;
returnfalse; // Doesn't fall in any of the above cases
}
staticSet<Pair<LineSegement,LineSegement>> notwice = newHashSet<Pair<LineSegement,LineSegement>>();
staticbooleanintersection(LineSegementa,LineSegementb) {
if(notwice.contains(newPair<LineSegement,LineSegement>(a,b))) {
returnfalse;
}else {
notwice.add(newPair<LineSegement,LineSegement>(a,b));
}
returnintersection(a.a, a.b, b.a, b.b);
}
publicStringtoString() {
return"("+this.x + ","+ this.y + ","+this.index+")";
}
booleaneq(Pointq) {
if(q.x == this.x && q.y == this.y) {
returntrue;
}
returnfalse;
}
@Override
publicintcompareTo(Pointarg0) {
if(arg0.x == this.x) {
returnDouble.compare(this.y, this.y);
}elseif(this.x > arg0.x) {
return -1;
}else {
return1;
}
//return Double.compare(this.x, arg0.x);
//return 0;
>>>>>>> parentofc6da410... LongProofing
}
}
classLineSegement {
Pointa,b;
publicLineSegement(Pointa,Pointb) {
if(a.x > b.x) {
this.a = b;
this.b = a;
}else {
this.a = a;
this.b = b;
}
}
publicdoubleatX(doublex) {
if(this.a.y == this.b.y) { // Straight
returnthis.a.y;
}else {
returnthis.a.y * (x/this.a.x);
}
}
<<<<<<< HEAD
<<<<<<< HEAD
<<<<<<< HEAD
<<<<<<< HEAD
publicPointatX_(doublex) {
returnnewPoint(x,this.atX(x));
}
=======
=======
>>>>>>> parentofc6da410... LongProofing
=======
>>>>>>> parentofc6da410... LongProofing
=======
>>>>>>> parentofc6da410... LongProofing
publicPointatX_(doublex) {
returnnewPoint(x,this.atX(x));
}
/*
>>>>>>> parentofc6da410... LongProofing
publicStringtoString() {
returnthis.a.toString() + " -- "+this.b.toString();
}
}