From f8848d7ac47eb49089129691f566dce38b6b9ab4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EB=AA=85=EC=A4=80?= <86913355+mjj111@users.noreply.github.com> Date: Fri, 9 Aug 2024 17:48:37 +0900 Subject: [PATCH] 2024-08-09 --- ...\355\202\250\353\260\260\353\213\254.java" | 86 +++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 "mjj111/\354\271\230\355\202\250\353\260\260\353\213\254.java" diff --git "a/mjj111/\354\271\230\355\202\250\353\260\260\353\213\254.java" "b/mjj111/\354\271\230\355\202\250\353\260\260\353\213\254.java" new file mode 100644 index 0000000..a386d9c --- /dev/null +++ "b/mjj111/\354\271\230\355\202\250\353\260\260\353\213\254.java" @@ -0,0 +1,86 @@ +import java.io.*; +import java.util.*; + + +public class 치킨배달 { + + static int N, M; + static int[][] graph; + static ArrayList person; + static ArrayList chicken; + static int answer; + static boolean[] open; + private static int CHICKEN = 2; + private static int HOME = 1; + + public static void main(String[] args) throws Exception { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + StringTokenizer st = new StringTokenizer(br.readLine()); + + N = Integer.parseInt(st.nextToken()); + M = Integer.parseInt(st.nextToken()); + + graph = new int[N][N]; + person = new ArrayList<>(); + chicken = new ArrayList<>(); + for (int i = 0; i < N; i++) { + st = new StringTokenizer(br.readLine()); + for (int j = 0; j < N; j++) { + graph[i][j] = Integer.parseInt(st.nextToken()); + + if (graph[i][j] == HOME) { + person.add(new Point(i, j)); + } + if (graph[i][j] == CHICKEN) { + chicken.add(new Point(i, j)); + } + } + } + + answer = Integer.MAX_VALUE; + open = new boolean[chicken.size()]; + + DFS(0, 0); + } + + public static void DFS(int start, int cnt) { + if(cnt != M) { + for (int i = start; i < chicken.size(); i++) { + open[i] = true; + DFS(i + 1, cnt + 1); + open[i] = false; + } + return; + } + + int res = 0; + for (int i = 0; i < person.size(); i++) { + int minimum = Integer.MAX_VALUE; + + for (int j = 0; j < chicken.size(); j++) { + if (open[j]) { + int distance = getDistance(i, j); + minimum = Math.min(minimum, distance); + } + } + + res += minimum; + } + answer = Math.min(answer, res); + } + + private static int getDistance(final int i, final int j) { + return Math.abs(person.get(i).x - chicken.get(j).x) + + Math.abs(person.get(i).y - chicken.get(j).y); + } + + private static class Point { + int x; + int y; + + Point(int x, int y) { + this.x = x; + this.y = y; + } + } +} \ No newline at end of file