-
Notifications
You must be signed in to change notification settings - Fork 0
/
Asteroids.java
142 lines (132 loc) · 3.31 KB
/
Asteroids.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
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.io.*;
import java.math.*;
import java.text.*;
import java.util.*;
import java.util.regex.*;
/*
br = new BufferedReader(new FileReader("input.txt"));
pw = new PrintWriter(new BufferedWriter(new FileWriter("output.txt")));
br = new BufferedReader(new InputStreamReader(System.in));
pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
*/
public class Asteroids {
private static BufferedReader br;
private static StringTokenizer st;
private static PrintWriter pw;
public static void main(String[] args) throws IOException {
br = new BufferedReader(new InputStreamReader(System.in));
pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
//int qq = 1;
//int qq = Integer.MAX_VALUE;
int qq = readInt();
for(int casenum = 1; casenum <= qq; casenum++) {
int n = readInt();
int s = readInt();
int[] x = new int[n];
int[] y = new int[n];
int[] r = new int[n];
Map<State, ArrayList<Integer>> map = new HashMap<State, ArrayList<Integer>>();
for(int i = 0; i < n; i++) {
x[i] = readInt();
y[i] = readInt();
r[i] = readInt();
State key = new State(x[i] / 100, y[i] / 100);
if(!map.containsKey(key)) {
map.put(key, new ArrayList<Integer>());
}
map.get(key).add(i);
}
int ret = 0;
while(s-- > 0) {
int ax = readInt();
int ay = readInt();
boolean can = false;
int kx = ax / 100;
int ky = ay / 100;
for(int dx = -1; !can && dx <= 1; dx++) {
for(int dy = -1; !can && dy <= 1; dy++) {
State key = new State(kx + dx, ky + dy);
ArrayList<Integer> value = map.get(key);
if(value != null) {
for(int out: value) {
int xx = x[out] - ax;
int yy = y[out] - ay;
if(xx*xx+yy*yy <= r[out]*r[out]) {
can = true;
break;
}
}
}
}
}
if(can) {
ret++;
}
}
pw.println("Case " + casenum + ": " + ret);
}
exitImmediately();
}
static class State {
public int x,y;
public State(int a, int b) {
x=a;
y=b;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + x;
result = prime * result + y;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
State other = (State) obj;
if (x != other.x)
return false;
if (y != other.y)
return false;
return true;
}
}
private static void exitImmediately() {
pw.close();
System.exit(0);
}
private static long readLong() throws IOException {
return Long.parseLong(nextToken());
}
private static double readDouble() throws IOException {
return Double.parseDouble(nextToken());
}
private static int readInt() throws IOException {
return Integer.parseInt(nextToken());
}
private static String nextLine() throws IOException {
if(!br.ready()) {
exitImmediately();
}
st = null;
return br.readLine();
}
private static String nextToken() throws IOException {
while(st == null || !st.hasMoreTokens()) {
if(!br.ready()) {
exitImmediately();
}
st = new StringTokenizer(br.readLine().trim());
}
return st.nextToken();
}
}