-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSurgicalStrike.java
151 lines (116 loc) · 3.05 KB
/
SurgicalStrike.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
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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class SurgicalStrike {
// input parameters
int n;
int m;
int t;
int base_x[], base_y[], baseId[];
int target_x[], target_y[], targetId[];
int baseX, baseY;
// output parameters
int maxTarget;
int distSourceToTarget[][];
int distTargetToBase[];
int distTotal[][];
public SurgicalStrike(int n, int m, int t) {
this.n = n;
this.m = m;
this.t = t;
base_x = new int[n];
base_y = new int[n];
baseId = new int[n];
target_x = new int[m];
target_y = new int[m];
targetId = new int[m];
maxTarget = 0;
distSourceToTarget = new int[n][m];
distTargetToBase = new int[m];
distTotal = new int[n][m];
}
void getData() throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
for(int i=0; i<n; i++) {
String s = br.readLine();
String[] temp = s.split(" ");
base_x[i] = Integer.parseInt(temp[0]);
base_y[i] = Integer.parseInt(temp[1]);
baseId[i] = Integer.parseInt(temp[2]);
}
for(int i=0; i<m; i++) {
String s = br.readLine();
String[] temp = s.split(" ");
target_x[i] = Integer.parseInt(temp[0]);
target_y[i] = Integer.parseInt(temp[1]);
targetId[i] = Integer.parseInt(temp[2]);
}
String s = br.readLine();
String[] temp = s.split("\\s+");
baseX = Integer.parseInt(temp[0]);
baseY = Integer.parseInt(temp[1]);
}
int getManhatanDist(int x1, int y1, int x2, int y2) {
return (x1-x2)+(y1-y2);
}
int getPrimeFactor(int dist, int id) {
if(dist%2 == 0) {
if(id<2)
return 2;
}
for(int i=3; i <= Math.sqrt(dist); i += 2) {
if(dist%i == 0) {
if(id<i)
return i;
}
}
return id;
}
void calcSourceToTarget() {
for(int i=0; i<n; i++) {
for(int j=0; j<m; j++) {
int dist = getManhatanDist(base_x[i], base_y[j], target_x[j], target_y[j]);
distSourceToTarget[i][j] = getPrimeFactor(dist, baseId[i]);
}
}
}
void calcTargetToBase() {
for(int i=0; i<m; i++)
distTargetToBase[i] = getManhatanDist(target_x[i], target_y[i], baseX, baseY);
}
void calcTotalDist() {
calcSourceToTarget();
calcTargetToBase();
for(int i=0; i<n; i++) {
for(int j=0; j<m; j++) {
distTotal[i][j] = distSourceToTarget[i][j] + distTargetToBase[j];
}
}
}
int getMaxTarget() {
calcTotalDist();
for(int i=0; i<n; i++) {
for(int j=0; j<m; j++) {
if(distTotal[i][j]<t) {
//System.out.println(distTotal[i][j]);
maxTarget = maxTarget+1;
}
}
}
return maxTarget/2;
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str = br.readLine();
String[] arr = str.split("\\s+");
int n = 0, m = 0, t = 0;
if(arr.length==3) {
n = Integer.parseInt(arr[0]);
m = Integer.parseInt(arr[1]);
t = Integer.parseInt(arr[2]);
}
SurgicalStrike obj = new SurgicalStrike(n, m ,t);
obj.getData();
System.out.println(obj.getMaxTarget());
}
}