728x90
https://www.acmicpc.net/problem/17135
package a0813;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.StringTokenizer;
public class Main_bj_17135_캐슬디펜스_서울_12반_이서영 {
static int n, m, d;
static StringTokenizer st;
static int num;
static ArrayList<Pair> enemies = new ArrayList<>();
static ArrayList<Pair> emptyPos = new ArrayList<>();
static int max = 0;
static class Pair {
int x, y;
Pair(int x, int y) {
this.x = x;
this.y = y;
}
}
public static int simulate(ArrayList<Pair> posList, ArrayList<Pair> tmpEnemies) {
int death = 0;
while (!tmpEnemies.isEmpty()) {
HashSet<Pair> hs = new HashSet<>();
for (int i = 0; i < posList.size(); i++) {
int px = posList.get(i).x;
int py = posList.get(i).y;
int mindist = Integer.MAX_VALUE;
int mj = -1;
int mex = -1;
int mey = -1;
for (int j = 0; j < tmpEnemies.size(); j++) {
int ex = tmpEnemies.get(j).x;
int ey = tmpEnemies.get(j).y;
int dist = Math.abs(px - ex) + Math.abs(py - ey);
if (dist <= d) {
if (mindist > dist) {
mj = j;
mex = ex;
mey = ey;
mindist = dist;
} else if (mindist == dist) {
if (mey > ey) {
mj = j;
mex = ex;
mey = ey;
}
}
}
}
if (mj != -1) {
hs.add(tmpEnemies.get(mj));
}
}
LinkedList<Pair> ls = new LinkedList<>(hs);
for (Pair p : ls) {
death++;
tmpEnemies.remove(p);
//hs = new HashSet<>();
hs.clear();
}
for (int t = 0; t < tmpEnemies.size(); t++) {
if ((tmpEnemies.get(t).x + 1) >= n) {
tmpEnemies.remove(t);
t--;
} else {
tmpEnemies.get(t).x = tmpEnemies.get(t).x + 1;
}
}
}
return death;
}
// 궁수의 위치를 조합으로 구한다.
public static void decidePos(ArrayList<Pair> posList, boolean[] selected, int idx) {
if (posList.size() == 3) {
ArrayList<Pair> tmpEnemies = new ArrayList<>();
for (int i = 0; i < enemies.size(); i++) {
tmpEnemies.add(new Pair(enemies.get(i).x, enemies.get(i).y));
} // *주의* Pair도 다시 할당 해주어야함!!!
max = Math.max(max, simulate(posList, tmpEnemies));
return;
}
for (int i = idx; i < emptyPos.size(); i++) {
if (!selected[i]) {
selected[i] = true;
posList.add(emptyPos.get(i));
decidePos(posList, selected, i + 1);
selected[i] = false;
posList.remove(posList.size() - 1);
}
}
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
st = new StringTokenizer(br.readLine());
n = Integer.parseInt(st.nextToken());
m = Integer.parseInt(st.nextToken());
d = Integer.parseInt(st.nextToken());
for (int i = 0; i < n; i++) {
st = new StringTokenizer(br.readLine());
for (int j = 0; j < m; j++) {
num = Integer.parseInt(st.nextToken());
if (num == 1) {
enemies.add(new Pair(i, j));
}
}
}
for (int i = 0; i < m; i++) {
emptyPos.add(new Pair(n, i));
}
decidePos(new ArrayList<>(), new boolean[emptyPos.size()], 0);
System.out.println(max);
}
}
- java는 Pair클래스를 만들었으면 꼭 파라미터로 보내주기 전에 새로 초기화를 해주어야한다.
'백준 > 브루트포스' 카테고리의 다른 글
백설공주와일곱난쟁이-3040번(java) (0) | 2021.08.15 |
---|---|
도영이가만든맛있는음식-2961번(java) (0) | 2021.08.15 |
치킨배달-15686번(java) (0) | 2021.08.15 |
테트로미노 (0) | 2021.02.07 |
N-Queen (0) | 2021.02.05 |