From c2a81c4845a315b9387888cd462c34d56692756d Mon Sep 17 00:00:00 2001 From: Ukj0ng <90972240+Ukj0ng@users.noreply.github.com> Date: Sun, 21 Sep 2025 22:49:26 +0900 Subject: [PATCH] =?UTF-8?q?[20250921]=20BOJ=20/=20G4=20/=20=EB=B9=A0?= =?UTF-8?q?=EB=A5=B8=20=EB=AC=B4=EC=9E=91=EC=9C=84=20=EB=A9=94=EC=8B=9C?= =?UTF-8?q?=EC=A7=80=20=EC=A0=84=EB=8B=AC=20/=20=ED=95=9C=EC=A2=85?= =?UTF-8?q?=EC=9A=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...4\354\247\200 \354\240\204\353\213\254.md" | 85 +++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 "Ukj0ng/202509/21 BOJ G4 \353\271\240\353\245\270 \353\254\264\354\236\221\354\234\204 \353\251\224\354\213\234\354\247\200 \354\240\204\353\213\254.md" diff --git "a/Ukj0ng/202509/21 BOJ G4 \353\271\240\353\245\270 \353\254\264\354\236\221\354\234\204 \353\251\224\354\213\234\354\247\200 \354\240\204\353\213\254.md" "b/Ukj0ng/202509/21 BOJ G4 \353\271\240\353\245\270 \353\254\264\354\236\221\354\234\204 \353\251\224\354\213\234\354\247\200 \354\240\204\353\213\254.md" new file mode 100644 index 00000000..6b4e65bb --- /dev/null +++ "b/Ukj0ng/202509/21 BOJ G4 \353\271\240\353\245\270 \353\254\264\354\236\221\354\234\204 \353\251\224\354\213\234\354\247\200 \354\240\204\353\213\254.md" @@ -0,0 +1,85 @@ +``` +import java.io.*; +import java.util.*; + +public class Main { + private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + private static final BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); + private static int[][] groups = { + {0, 0}, {1, 2}, {3, 4}, {5, 6}, {7, 8}, {9, 10}, {11, 12} + }; + private static int[] combination, firstInGroup; + private static boolean[] visited; + private static int[][] times; + private static int minTime = Integer.MAX_VALUE; + + public static void main(String[] args) throws IOException { + init(); + + DFS(1); + bw.write(minTime + "\n"); + bw.flush(); + bw.close(); + br.close(); + } + + private static void init() throws IOException { + times = new int[13][13]; + combination = new int[7]; + firstInGroup = new int[7]; + visited = new boolean[7]; + + for (int i = 1; i <= 12; i++) { + StringTokenizer st = new StringTokenizer(br.readLine()); + for (int j = 1; j <= 12; j++) { + times[i][j] = Integer.parseInt(st.nextToken()); + } + } + } + + private static void DFS(int index) { + if (index == 7) { + for (int mask = 0; mask < (1 << 6); mask++) { + for (int i = 1; i <= 6; i++) { + firstInGroup[i] = ((mask >> (i - 1)) & 1) == 0 ? 0 : 1; + } + int totalTime = cal(); + minTime = Math.min(minTime, totalTime); + } + return; + } + + for (int i = 1; i <= 6; i++) { + if (!visited[i]) { + visited[i] = true; + combination[index] = i; + DFS(index + 1); + visited[i] = false; + } + } + } + + private static int cal() { + int totalTime = 0; + int prevStudent = -1; + + for (int i = 1; i <= 6; i++) { + int index = combination[i]; + int[] group = groups[index]; + int first = group[firstInGroup[index]]; + int second = group[1 - firstInGroup[index]]; + + if (i == 1) { + totalTime += times[first][second]; + prevStudent = second; + } else { + totalTime += times[prevStudent][first]; + totalTime += times[first][second]; + prevStudent = second; + } + } + + return totalTime; + } +} +```