diff --git "a/JHLEE325/202601/23 BOJ G4 \353\260\260\354\227\264 \353\217\214\353\246\254\352\270\260 4.md" "b/JHLEE325/202601/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..5a8435d9 --- /dev/null +++ "b/JHLEE325/202601/23 BOJ G4 \353\260\260\354\227\264 \353\217\214\353\246\254\352\270\260 4.md" @@ -0,0 +1,108 @@ +```java +import java.io.*; +import java.util.*; + + +public class Main { + static int N, M, K; + static int[][] originalBoard; + static Operation[] ops; + static boolean[] visited; + static int[] p; + static int minVal = Integer.MAX_VALUE; + + static class Operation { + int r, c, s; + Operation(int r, int c, int s) { + this.r = r; this.c = c; this.s = s; + } + } + + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + StringTokenizer st = new StringTokenizer(br.readLine()); + + N = Integer.parseInt(st.nextToken()); + M = Integer.parseInt(st.nextToken()); + K = Integer.parseInt(st.nextToken()); + + originalBoard = new int[N + 1][M + 1]; + for (int i = 1; i <= N; i++) { + st = new StringTokenizer(br.readLine()); + for (int j = 1; j <= M; j++) { + originalBoard[i][j] = Integer.parseInt(st.nextToken()); + } + } + + ops = new Operation[K]; + for (int i = 0; i < K; i++) { + st = new StringTokenizer(br.readLine()); + ops[i] = new Operation( + Integer.parseInt(st.nextToken()), + Integer.parseInt(st.nextToken()), + Integer.parseInt(st.nextToken()) + ); + } + + visited = new boolean[K]; + p = new int[K]; + permutation(0); + + System.out.println(minVal); + } + + static void permutation(int cnt) { + if (cnt == K) { + int[][] copyBoard = copy(); + for (int i = 0; i < K; i++) { + rotate(copyBoard, ops[p[i]]); + } + calculateMin(copyBoard); + return; + } + for (int i = 0; i < K; i++) { + if (visited[i]) continue; + visited[i] = true; + p[cnt] = i; + permutation(cnt + 1); + visited[i] = false; + } + } + + static void rotate(int[][] board, Operation op) { + int r = op.r; + int c = op.c; + int s = op.s; + + for (int i = 1; i <= s; i++) { + int top = r - i; + int left = c - i; + int bottom = r + i; + int right = c + i; + + int temp = board[top][left]; + + for (int row = top; row < bottom; row++) board[row][left] = board[row + 1][left]; + for (int col = left; col < right; col++) board[bottom][col] = board[bottom][col + 1]; + for (int row = bottom; row > top; row--) board[row][right] = board[row - 1][right]; + for (int col = right; col > left + 1; col--) board[top][col] = board[top][col - 1]; + + board[top][left + 1] = temp; + } + } + + static int[][] copy() { + int[][] res = new int[N + 1][M + 1]; + for (int i = 1; i <= N; i++) res[i] = originalBoard[i].clone(); + return res; + } + + static void calculateMin(int[][] board) { + for (int i = 1; i <= N; i++) { + int sum = 0; + for (int j = 1; j <= M; j++) sum += board[i][j]; + minVal = Math.min(minVal, sum); + } + } +} +```