From d9c98226bf19e50cbb8a3588aadce895ed9c0b37 Mon Sep 17 00:00:00 2001 From: zinnnn37 Date: Mon, 23 Feb 2026 22:30:06 +0900 Subject: [PATCH] =?UTF-8?q?[20260223]=20BOJ=20/=20G4=20/=20=EB=B0=B0?= =?UTF-8?q?=EC=97=B4=20=EB=8F=8C=EB=A6=AC=EA=B8=B0=204=20/=20=EA=B9=80?= =?UTF-8?q?=EB=AF=BC=EC=A7=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\353\217\214\353\246\254\352\270\260 4.md" | 120 ++++++++++++++++++ 1 file changed, 120 insertions(+) create mode 100644 "zinnnn37/202602/23 BOJ G4 \353\260\260\354\227\264 \353\217\214\353\246\254\352\270\260 4.md" diff --git "a/zinnnn37/202602/23 BOJ G4 \353\260\260\354\227\264 \353\217\214\353\246\254\352\270\260 4.md" "b/zinnnn37/202602/23 BOJ G4 \353\260\260\354\227\264 \353\217\214\353\246\254\352\270\260 4.md" new file mode 100644 index 00000000..9b19ae4e --- /dev/null +++ "b/zinnnn37/202602/23 BOJ G4 \353\260\260\354\227\264 \353\217\214\353\246\254\352\270\260 4.md" @@ -0,0 +1,120 @@ +```java +import java.io.*; +import java.util.StringTokenizer; + +public class BJ_17406_배열_돌리기_4 { + + private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + private static final BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); + private static StringTokenizer st; + + private static int N, M, K, ans; + private static int[] order; + private static int[][] matrix, ops; + private static boolean[] visited; + + public static void main(String[] args) throws IOException { + init(); + sol(); + bw.write(ans + "\n"); + bw.flush(); + bw.close(); + } + + private static void init() throws IOException { + st = new StringTokenizer(br.readLine()); + N = Integer.parseInt(st.nextToken()); + M = Integer.parseInt(st.nextToken()); + K = Integer.parseInt(st.nextToken()); + ans = Integer.MAX_VALUE; + + matrix = new int[N][M]; + for (int i = 0; i < N; i++) { + st = new StringTokenizer(br.readLine()); + for (int j = 0; j < M; j++) { + matrix[i][j] = Integer.parseInt(st.nextToken()); + } + } + + ops = new int[K][3]; + for (int i = 0; i < K; i++) { + st = new StringTokenizer(br.readLine()); + ops[i][0] = Integer.parseInt(st.nextToken()) - 1; + ops[i][1] = Integer.parseInt(st.nextToken()) - 1; + ops[i][2] = Integer.parseInt(st.nextToken()); + } + + order = new int[K]; + visited = new boolean[K]; + } + + private static void sol() throws IOException { + perm(0); + } + + private static void perm(int depth) { + if (depth == K) { + int[][] tmp = new int[N][M]; + for (int i = 0; i < N; i++) { + System.arraycopy(matrix[i], 0, tmp[i], 0, M); + } + + // order 순서대로 회전 + for (int i = 0; i < K; i++) { + int idx = order[i]; + rotate(tmp, ops[idx][0], ops[idx][1], ops[idx][2]); + } + + int minRowSum = Integer.MAX_VALUE; + for (int i = 0; i < N; i++) { + int sum = 0; + for (int j = 0; j < M; j++) { + sum += tmp[i][j]; + } + minRowSum = Math.min(minRowSum, sum); + } + ans = Math.min(ans, minRowSum); + return; + } + + for (int i = 0; i < K; i++) { + if (!visited[i]) { + visited[i] = true; + order[depth] = i; + perm(depth + 1); + visited[i] = false; + } + } + } + + private static void rotate(int[][] arr, int r, int c, int s) { + for (int l = 1; l <= s; l++) { + int top = r - l; + int bottom = r + l; + int left = c - l; + int right = c + l; + + int tmp = arr[top][left]; + + // 하 -> 상 + for (int i = top; i < bottom; i++) { + arr[i][left] = arr[i + 1][left]; + } + // 오 -> 왼 + for (int j = left; j < right; j++) { + arr[bottom][j] = arr[bottom][j + 1]; + } + // 상 -> 하 + for (int i = bottom; i > top; i--) { + arr[i][right] = arr[i - 1][right]; + } + // 왼 -> 오 + for (int j = right; j > left + 1; j--) { + arr[top][j] = arr[top][j - 1]; + } + arr[top][left + 1] = tmp; + } + } + +} +``` \ No newline at end of file