풀이
조합을 사용하여 해결했습니다.
처음에 순열로 접근했다가 시간 초과가 나서 헤매었었던 문제입니다.
코드
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
public class Main {
public static class Info {
int y, x;
public Info(int y, int x) {
super();
this.y = y;
this.x = x;
}
}
public static int N, M, answer;
public static boolean[] isSelected;
public static List<Info> chickenList;
public static List<Info> homeList;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine(), " ");
N = Integer.parseInt(st.nextToken());
M = Integer.parseInt(st.nextToken());
chickenList = new ArrayList<Info>();
homeList = new ArrayList<Info>();
for (int i = 1; i <= N; ++i) {
st = new StringTokenizer(br.readLine(), " ");
for (int j = 1; j <= N; ++j) {
int type = Integer.parseInt(st.nextToken());
if (type == 1) {
homeList.add(new Info(i, j));
}
else if (type == 2) {
chickenList.add(new Info(i, j));
}
}
}
isSelected = new boolean[chickenList.size()];
answer = Integer.MAX_VALUE;
go(0, 0);
System.out.println(answer);
}
public static void go(int cnt, int idx) {
if (cnt == M) {
int totalDist = 0;
for (int i = 0; i < homeList.size(); ++i) {
Info home = homeList.get(i);
int minDist = Integer.MAX_VALUE;
for (int j = 0; j < chickenList.size(); ++j) {
if (!isSelected[j]) {
continue;
}
Info chicken = chickenList.get(j);
int dist = Math.abs(home.y - chicken.y) + Math.abs(home.x - chicken.x);
minDist = (minDist > dist) ? dist : minDist;
}
totalDist += minDist;
}
answer = (answer > totalDist) ? totalDist : answer;
return;
}
for (int i = idx; i < chickenList.size(); ++i) {
isSelected[i] = true;
go(cnt + 1, i + 1);
isSelected[i] = false;
}
}
}
'알고리즘 > 백준' 카테고리의 다른 글
[Java] BOJ 10026번 적록색약 (0) | 2021.05.14 |
---|---|
[Java] BOJ 14500번 테트로미노 (0) | 2021.05.13 |
[Java] BOJ 1759번 암호 만들기 (0) | 2021.05.11 |
[Java] BOJ 14503번 로봇 청소기 (0) | 2021.05.10 |
[Java] BOJ 11722번 가장 긴 감소하는 부분 수열 (0) | 2021.05.07 |