-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMain.java
96 lines (81 loc) Β· 2.59 KB
/
Main.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
package Simulation.P17135;
import java.io.*;
import java.util.*;
public class Main {
static int N, M, D, max;
static int[][] map;
static int[] shooter = new int[3];
static ArrayList<Pos> enemies = new ArrayList<>();
public static void main(String[] args) throws Exception {
System.setIn(new FileInputStream("src/Simulation/P17135/input.txt"));
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
N = Integer.parseInt(st.nextToken());
M = Integer.parseInt(st.nextToken());
D = Integer.parseInt(st.nextToken());
map = new int[N+1][M];
for (int i = 0; i < N; i++) {
st = new StringTokenizer(br.readLine());
for (int j = 0; j < M; j++) {
map[i][j] = Integer.parseInt(st.nextToken());
}
}
placeShooter(0, 0);
System.out.println(max);
}
static void initEnemies() {
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
if (map[i][j] == 1) enemies.add(new Pos(i, j));
}
}
}
static void placeShooter(int idx, int cnt) {
if (cnt == 3) {
initEnemies();
play();
return;
}
for (int i = idx; i < M; i++) {
if (map[N][i] == 0) {
shooter[cnt] = i;
placeShooter(i+1, cnt+1);
}
}
}
static void play() {
PriorityQueue<Pos> pq = new PriorityQueue<>((o1, o2) -> o1.dist == o2.dist ? o1.j-o2.j : o1.dist-o2.dist);
HashSet<Pos> kill = new HashSet<>();
int cnt = 0;
while (!enemies.isEmpty()) {
for (int s : shooter) {
for (Pos e : enemies) {
if (e.setDist(N, s) <= D) pq.offer(e);
}
if (!pq.isEmpty()) kill.add(pq.poll());
pq.clear();
}
for (Pos k : kill) {
enemies.remove(k);
cnt++;
}
kill.clear();
for (int i = 0; i < enemies.size(); i++) {
if (enemies.get(i).i+1 == N) enemies.remove(i--);
else enemies.get(i).i ++;
}
}
max = Math.max(max, cnt);
}
static class Pos {
int i, j, dist;
public Pos(int i, int j) {
this.i = i;
this.j = j;
}
public int setDist(int r, int c) {
dist = Math.abs(i-r) + Math.abs(j-c);
return dist;
}
}
}