-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHumanCannonBall.java
119 lines (103 loc) · 4.26 KB
/
HumanCannonBall.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
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
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
public class HumanCannonBall {
public static void main(String[] args) throws IOException {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
PrintWriter writer = new PrintWriter(System.out);
int index = 0;
String[] inputs = bf.readLine().split(" ");
Coordinates start = new Coordinates(Double.parseDouble(inputs[0]),
Double.parseDouble(inputs[1]), false, index);
index++;
inputs = bf.readLine().split(" ");
ArrayList<Coordinates> coordinates = new ArrayList<>();
ArrayList<IntegerTriple> edgeList = new ArrayList<>();
coordinates.add(start); // first item is the source
int numOfCannons = Integer.parseInt(bf.readLine());
int runRate = 5;
int cannonOverHead = 2;
for (int i = 0; i < numOfCannons ; i++) {
String[] cannonInputs = bf.readLine().split(" ");
//next few items is the cannons
coordinates.add(new Coordinates(Double.parseDouble(cannonInputs[0]),
Double.parseDouble(cannonInputs[1]),true, index));
index++;
}
Coordinates end = new Coordinates(Double.parseDouble(inputs[0]),
Double.parseDouble(inputs[1]), false, index);
coordinates.add(end);// last item is the destination
for (Coordinates temp1 : coordinates) {
for (Coordinates temp2 : coordinates) {
if(temp1.x != temp2.x && temp1.y != temp2.y) { //not the same coordinate
//calculate difference in distance between the coordinate
double x = temp1.x - temp2.x; // a - b or b - a no diff since going to square
double y = temp1.y - temp2.y;
double distance = Math.sqrt(x * x + y * y);
double timeNeeded = distance / runRate;
double cannonTime = Double.POSITIVE_INFINITY;
if (temp1.isCannon) {
//cannon time to some other coordinate need to absolute in case cannon overshoots
cannonTime = cannonOverHead + (Math.abs(distance - 50) / runRate);
}
edgeList.add(new IntegerTriple(temp1, temp2, Math.min(timeNeeded, cannonTime)));
// System.out.println(Math.min(timeNeeded, cannonTime));
}
}
}
ArrayList<Double> D = new ArrayList<>();
//initialize SSSP
D.addAll(Collections.nCopies(coordinates.size(), Double.POSITIVE_INFINITY));
D.set(coordinates.get(0).index, 0.0);
//start of bellman Ford
for (int i = 0; i < coordinates.size() - 1; i++) {
for (int j = 0; j < edgeList.size(); j++) {
IntegerTriple temp = edgeList.get(j);
if(D.get(temp.first.index) != Double.POSITIVE_INFINITY
&& D.get(temp.second.index) > D.get(temp.first.index) + temp.time) {
D.set(temp.second.index, D.get(temp.first.index) + temp.time);
}
}
}
writer.println(D.get(coordinates.size() - 1));
writer.flush();
writer.close();
bf.close();
}
}
//coordinates
class Coordinates {
Double x;
Double y;
boolean isCannon;
int index;
public Coordinates(double x, double y, boolean isCannon, int index) {
this.x = x;
this.y = y;
this.isCannon = isCannon;
this.index = index;
}
}
class IntegerTriple implements Comparable<IntegerTriple>{
Coordinates first;
Coordinates second;
double time;
public IntegerTriple(Coordinates i, Coordinates j, double time){
this.first = i;
this.second = j;
this.time = time;
}
@Override
public int compareTo(IntegerTriple o) {
if(this.time < o.time) {
return -1;
} else if(this.time > o.time) {
return 1;
} else {
return 0;
}
}
}