|
| 1 | +import java.io.BufferedReader; |
| 2 | +import java.io.IOException; |
| 3 | +import java.io.InputStreamReader; |
| 4 | +import java.util.StringTokenizer; |
| 5 | +import java.util.List; |
| 6 | +import java.util.ArrayList; |
| 7 | + |
| 8 | +// 5 3 |
| 9 | +// 0 0 1 0 0 |
| 10 | +// 0 0 2 0 1 |
| 11 | +// 0 1 2 0 0 |
| 12 | +// 0 0 1 0 0 |
| 13 | +// 0 0 0 0 2 |
| 14 | + |
| 15 | +// 5 2 |
| 16 | +// 0 2 0 1 0 |
| 17 | +// 1 0 1 0 0 |
| 18 | +// 0 0 0 0 0 |
| 19 | +// 2 0 0 1 1 |
| 20 | +// 2 2 0 1 2 |
| 21 | + |
| 22 | +// 5 1 |
| 23 | +// 1 2 0 2 1 |
| 24 | +// 1 2 0 2 1 |
| 25 | +// 1 2 0 2 1 |
| 26 | +// 1 2 0 2 1 |
| 27 | +// 1 2 0 2 1 |
| 28 | + |
| 29 | +public class Main { |
| 30 | + |
| 31 | + static int[][] cityMap; |
| 32 | + static List<int[]> eachHousePosition; |
| 33 | + static List<int[]> chickenPosition; |
| 34 | + static int[][] output; |
| 35 | + static int totalChickenDistance = Integer.MAX_VALUE; |
| 36 | + |
| 37 | + |
| 38 | + public static void main(String[] args) throws IOException { |
| 39 | + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 40 | + StringTokenizer st = new StringTokenizer(br.readLine()); |
| 41 | + int N = Integer.parseInt(st.nextToken()); |
| 42 | + int M = Integer.parseInt(st.nextToken()); |
| 43 | + cityMap = new int[N][N]; |
| 44 | + eachHousePosition = new ArrayList<>(); |
| 45 | + chickenPosition = new ArrayList<>(); |
| 46 | + output = new int[M][2]; |
| 47 | + |
| 48 | + for (int i = 0; i < N; i++) { |
| 49 | + st = new StringTokenizer(br.readLine()); |
| 50 | + for (int j = 0; j < N; j++) { |
| 51 | + int value = Integer.parseInt(st.nextToken()); |
| 52 | + cityMap[i][j] = value; |
| 53 | + |
| 54 | + if (value == 1) { |
| 55 | + eachHousePosition.add(new int[]{i, j}); |
| 56 | + } else if (value == 2) { |
| 57 | + chickenPosition.add(new int[]{i, j}); |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + combination(0, 0, chickenPosition.size(), M); |
| 63 | + System.out.println(totalChickenDistance); |
| 64 | + |
| 65 | + } |
| 66 | + |
| 67 | + private static void combination(int start, int depth, int n, int r) { |
| 68 | + if (depth == r) { |
| 69 | + int totalDistance = calculateTotalChickenDistance(eachHousePosition, output); |
| 70 | + totalChickenDistance = Math.min(totalChickenDistance, totalDistance); |
| 71 | + return; |
| 72 | + } |
| 73 | + |
| 74 | + for (int i = start; i < n; i++) { |
| 75 | + output[depth] = chickenPosition.get(i); |
| 76 | + combination(i + 1, depth + 1, n, r); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + private static int calculateTotalChickenDistance(List<int[]> housePosition, int[][] output) { |
| 81 | + int totalDistance = 0; |
| 82 | + for (int[] house : housePosition) { |
| 83 | + int min = Integer.MAX_VALUE; |
| 84 | + for (int[] chickenShop : output) { |
| 85 | + min = Math.min(min, Math.abs(house[0] - chickenShop[0]) + Math.abs(house[1] - chickenShop[1])); |
| 86 | + } |
| 87 | + |
| 88 | + totalDistance += min; |
| 89 | + } |
| 90 | + |
| 91 | + return totalDistance; |
| 92 | + } |
| 93 | +} |
0 commit comments