|
| 1 | +import java.util.*; |
| 2 | + |
| 3 | +class Combination { |
| 4 | + private int n; |
| 5 | + private int r; |
| 6 | + private int[] now; // 현재 조합 |
| 7 | + private ArrayList<ArrayList<Node>> result; // 모든 조합 |
| 8 | + |
| 9 | + public ArrayList<ArrayList<Node>> getResult() { |
| 10 | + return result; |
| 11 | + } |
| 12 | + |
| 13 | + public Combination(int n, int r) { |
| 14 | + this.n = n; |
| 15 | + this.r = r; |
| 16 | + this.now = new int[r]; |
| 17 | + this.result = new ArrayList<ArrayList<Node>>(); |
| 18 | + } |
| 19 | + |
| 20 | + public void combination(ArrayList<Node> arr, int depth, int index, int target) { |
| 21 | + if (depth == r) { |
| 22 | + ArrayList<Node> temp = new ArrayList<>(); |
| 23 | + for (int i = 0; i < now.length; i++) { |
| 24 | + temp.add(arr.get(now[i])); |
| 25 | + } |
| 26 | + result.add(temp); |
| 27 | + return; |
| 28 | + } |
| 29 | + if (target == n) return; |
| 30 | + now[index] = target; |
| 31 | + combination(arr, depth + 1, index + 1, target + 1); |
| 32 | + combination(arr, depth, index, target + 1); |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +class Node { |
| 37 | + private int x; |
| 38 | + private int y; |
| 39 | + |
| 40 | + public Node(int x, int y) { |
| 41 | + this.x = x; |
| 42 | + this.y = y; |
| 43 | + } |
| 44 | + |
| 45 | + public int getX() { |
| 46 | + return this.x; |
| 47 | + } |
| 48 | + |
| 49 | + public int getY() { |
| 50 | + return this.y; |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +public class Main { |
| 55 | + |
| 56 | + public static int n, m; |
| 57 | + public static int[][] arr = new int[50][50]; |
| 58 | + public static ArrayList<Node> chicken = new ArrayList<>(); |
| 59 | + public static ArrayList<Node> house = new ArrayList<>(); |
| 60 | + |
| 61 | + public static int getSum(ArrayList<Node> candidates) { |
| 62 | + int result = 0; |
| 63 | + // 모든 집에 대하여 |
| 64 | + for (int i = 0; i < house.size(); i++) { |
| 65 | + int hx = house.get(i).getX(); |
| 66 | + int hy = house.get(i).getY(); |
| 67 | + // 가장 가까운 치킨 집을 찾기 |
| 68 | + int temp = (int) 1e9; |
| 69 | + for (int j = 0; j < candidates.size(); j++) { |
| 70 | + int cx = candidates.get(j).getX(); |
| 71 | + int cy = candidates.get(j).getY(); |
| 72 | + temp = Math.min(temp, Math.abs(hx - cx) + Math.abs(hy - cy)); |
| 73 | + } |
| 74 | + // 가장 가까운 치킨 집까지의 거리를 더하기 |
| 75 | + result += temp; |
| 76 | + } |
| 77 | + // 치킨 거리의 합 반환 |
| 78 | + return result; |
| 79 | + } |
| 80 | + |
| 81 | + public static void main(String[] args) { |
| 82 | + Scanner sc = new Scanner(System.in); |
| 83 | + |
| 84 | + n = sc.nextInt(); |
| 85 | + m = sc.nextInt(); |
| 86 | + |
| 87 | + for (int r = 0; r < n; r++) { |
| 88 | + for (int c = 0; c < n; c++) { |
| 89 | + arr[r][c] = sc.nextInt(); |
| 90 | + if (arr[r][c] == 1) house.add(new Node(r, c)); // 일반 집 |
| 91 | + else if (arr[r][c] == 2) chicken.add(new Node(r, c)); // 치킨집 |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + // 모든 치킨 집 중에서 m개의 치킨 집을 뽑는 조합 계산 |
| 96 | + Combination comb = new Combination(chicken.size(), m); |
| 97 | + comb.combination(chicken, 0, 0, 0); |
| 98 | + ArrayList<ArrayList<Node>> chickenList = comb.getResult(); |
| 99 | + |
| 100 | + // 치킨 거리의 합의 최소를 찾아 출력 |
| 101 | + int result = (int) 1e9; |
| 102 | + for (int i = 0; i < chickenList.size(); i++) { |
| 103 | + result = Math.min(result, getSum(chickenList.get(i))); |
| 104 | + } |
| 105 | + System.out.println(result); |
| 106 | + } |
| 107 | +} |
0 commit comments