-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWar.java
55 lines (51 loc) · 2.58 KB
/
War.java
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
/*
This is my solution the the Kattis problem War on Weather. Given a set of sattelites
and a set of points on the Earth, the problem asks how many points are visible
from at least one sattelite. My program uses the fact that a point will be visible
from a sattelite iff the angle SCP is <= 90 degrees, where S is the position of the
sattelite, C is the centre of the Earth and P is the point on the earth's surface.
*/
import java.util.*;
import java.math.*;
public class War {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNext()) {
int k = in.nextInt();//number of sattelites
int m = in.nextInt();//number of tropical depressions
if (k == 0 && m == 0) System.exit(0);
in.nextLine();
double[][] sattelites = new double[k][3];
double[][] depressions = new double[m][3];
String[] tmp;
for (int i = 0; i < k; i++) {
tmp = in.nextLine().split(" ");
sattelites[i][0] = Double.parseDouble(tmp[0]);
sattelites[i][1] = Double.parseDouble(tmp[1]);
sattelites[i][2] = Double.parseDouble(tmp[2]);
}
for (int i = 0; i < m; i++) {
tmp = in.nextLine().split(" ");
depressions[i][0] = Double.parseDouble(tmp[0]);
depressions[i][1] = Double.parseDouble(tmp[1]);
depressions[i][2] = Double.parseDouble(tmp[2]);
}
boolean[] hit = new boolean[m];//mark depresions as true when we find a sattelite that hits them
int numHit = 0;
for (int sattelite = 0; sattelite < k; sattelite++) {
for (int depression = 0; depression < m; depression++) {
if (!hit[depression]) {
double[] v1 = {0 - depressions[depression][0],0-depressions[depression][1],0-depressions[depression][2]};//vector from point to depression
double[] v2 = {sattelites[sattelite][0]-depressions[depression][0],sattelites[sattelite][1]-depressions[depression][1],sattelites[sattelite][2]-depressions[depression][2]};//vector from sattelite to depression
double dot = v1[0]*v2[0] + v1[1]*v2[1] + v1[2]*v2[2];
if (dot <= 0) {//check if the is obtuse.
hit[depression] = true;
numHit++;
}
}
}
}
System.out.println(numHit);
}
}
}